I want to covert the scriptlet part into java classes. Basically, there would be two classes consisting of the entity classes and the other class containing the method. The entity class contains all the table elements. My main motive is to convert the scriptlet part into JAVA class.Here is what I have so far:
<%#page import="java.sql.*"%>
<%#page import="connection.Dbconnect"%>
<%#page session="true" %>
<div id="page" class="container">
<div id="content">
<%
String user = session.getAttribute("username").toString();
ResultSet rs=null;
ResultSet rs1=null;
String s1,s2,s3,s4,s5,s6,s7;
try{
Connection con=Dbconnect.getconnection();
Statement st = con.createStatement();
Statement st2 = con.createStatement();
rs=st.executeQuery("select * from user where user_name = '"+user+"'");
if ( rs.next() ){
s1 = rs.getString(1);
s2 = rs.getString(2);
s3 = rs.getString(3);
s4 = rs.getString(4);
s5 = rs.getString(5);
s6 = rs.getString(6);
s7 = rs.getString(7);
%>
<div class="title">
<h2> <%=s4%> </h2>
<h2> My Issues</h2>
<span class="byline"><p><b>Assign Issues</b></p></span>
</div>
<div class= "spltable">
<table border-bottom=1 align=center style="text-align:center">
<thead>
<tr>
<th>Issue ID</th>
<th>Subject</th>
<th>Description</th>
<th>Department</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<%
rs1 = st2.executeQuery("select * from issue where user_id = '"+s5+"'");
while(rs1.next()){
%>
<tr>
<td><%= rs1.getString(1) %></td>
<td><%= rs1.getString(2) %></td>
<td align="justify"><%= rs1.getString(3) %></td>
<td><%= rs1.getString(10) %></td>
<td><%= rs1.getString(14) %></td>
<td><%= rs1.getString(7) %></td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
</div>
</div>
<%
}
}
catch(Exception e) {
out.println(e.getMessage());
}
%>
It's not necessary spring MVC for that (and to learn Spring MVC would take much more time and maybe neither the case to use here, despite is good to build robust applications, not the case here for what I see.... ), you create the class containing the logic, then a Servlet class will pass the result set to the Jsp page, an example on how to pass data from the Servlet to the Jsp can be found here: https://www.geeksforgeeks.org/getattribute-passing-data-from-server-to-jsp/
Related
I have recently started working with JSP and I am trying to pass rollno value from one page to another. However, it is always passing the last row rollno.
Below is my first JSP page from where I'm taking values and also feepay page where I retrieve those values. I have tried appending rollno to URL but that didn't work either.
Please help me with this.
<%
String id=session.getAttribute("rollno").toString();
int idd=Integer.parseInt(id);
%>
<table class="table table-striped">
<thead>
<tr>
<th>NAME</th>
<th>ROLL NO</th>
<th>FEE</th>
<th>FEE PAID</th>
<th>FEE PENDING</th>
<th>PAY FEE</th>
</tr>
<% while(resultset.next()){ %>
<TR>
<TD> <%= resultset.getString(1) %></td>
<TD> <%= resultset.getString(2) %></TD><% String rollno=resultset.getString(2).toString(); %>
<TD><%= resultset.getString(3) %></TD>
<td><%= resultset.getString(4) %></td>
<td><%= resultset.getString(5) %></td>
<TD>Pay Fee</TD>
</TR>
<% } %>
</thead>
</table>
1st : your overwriting the session in while loop so only you always getting last row value .
2nd : Just pass it like query string get parameter like below
<TD><a href="feepaypage.jsp?rollno=<%=resultset.getString(2) %>" >Pay Fee</a></TD>
3rd : Access the get parameter like below
<%
String id=request.getParameter("rollno").toString();
int idd=Integer.parseInt(id);
%>
I am getting ID from database in one place, here the code
<%
int id;
while(rsu.next()) {
id = rsu.getInt(1);
%>
<tr class="gradeA">
<td><%= rsu.getString(2) %></td>
<td><% rsu.getString(3) %></td>
<td><% rsu.getString(4); %></td>
<td class="center"><% rsu.getString(5); %></td>
<td class="center">
<select onchange="selected_action(this.value,<%= id %>)" id="approved">
<option >Action</option>
<option value="approve">Approve</option>
<option value="reject">Reject</option>
</select>
</td>
<td>
<button type="button" class="btn" data-toggle="modal" data-target="#myModal" onclick="btnClicked(<%= id %>)">Process</button>
</td>
</tr>
<%
}
%>
As from above code you can See I am invoke a bootstrap popup so I want same database id in my popup when I click on Process , here I want the Id;
<input type="button" name="Token" class="btn btn-info" id="first-btn" value="Tokanization" onClick="btnClicked('<%= id %>')" >
but its giving the error that
"Multiple annotations found at this line:
- Tag (input) should be an empty-element tag.
- The local variable id may not have been initialized"
Also I want to get same id in Servlet for further process
Thanks in Advance , Cheers :)
You have erros in these lines:
<td><%= rsu.getString(2) %></td>
<td><% rsu.getString(3) %></td>
<td><% rsu.getString(4); %></td>
<td class="center"><% rsu.getString(5); %></td>
If you put the = in the start of the "jsp tag" the output of the ivocation will be printed in the page. JSP compiler will generate the <%= CODE %> as an page.wirte() so in this case you dont use ";" in the end of your code.
If you dont put the = in the "jsp tag" the JSP compiler will include your code in the generated class. So you need to end your statements with ";".
You probably want something like :
<td><%= rsu.getString(2) %></td>
<td><%= rsu.getString(3) %></td>
<td><%= rsu.getString(4) %></td>
<td class="center"><%= rsu.getString(5) %></td>
I am developing a web application and I have a jsp page in which I have a table and few other things.All I want is to refresh the contents of table every 5 seconds.Below is my code of jsp page.Can anyone help me solve my problem.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page language="java" %>
<%# page import="java.sql.*" %>
<%# page import="java.util.Calendar" %>
<%# page import="java.text.DateFormat" %>
<%# page import="java.util.Date" %>
<%# page import="java.text.SimpleDateFormat" %>
<HTML>
<HEAD>
<TITLE>Welcome to Crevavi </TITLE>
</HEAD>
<div style="width:950px; height:900; padding:10px; border:10px ridge black;">
<body bgcolor="white"; border="3px">
<img src="Crevavi_Plain.jpg" background-color="white" width="100" height="25" style=float:right;/>
<h1 style=margin-left:2px;><font size="5"> Crevavi Web Application</font></h1>
<hr color="black">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var auto = setInterval( function ()
{
$("#result").load("NewTable.html #result");
}, 5000); // refresh every 5000 milliseconds
</script>
//I want only the below division to refresh every 5 seconds.
<div id="result" style="width:930px; height:500; padding:5px; border:5px ridge black;">
<%
int rowCount = 0;
/*Calendar cal = Calendar.getInstance();
Date date1=cal.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy,HH:mm:ss");
String formattedDate=dateFormat.format(date1);
System.out.println("Current time of the day using Calendar - 24 hour format: "+ formattedDate);
String[] values = formattedDate.split(",");
String date = values[0];
String time = values[1];*/
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/students","root","root");
Statement statement = connection.createStatement() ;
/* String sql = "INSERT INTO TEXTDATA (MachID,Date,Time,Text1,Text2,Text3,Text4,Text5,Text6,Text7,Text8,Text9,Text10,Text11,Text12) VALUES ('123','"+date+"','"+time+"','My','hello','thankyou','welcome','visit again','haha','good morning','sweet dreams','hi','hello','night','work')";
((java.sql.Statement) statement).executeUpdate(sql);*/
ResultSet resultset = statement.executeQuery("select * from textdata order by Date desc, Time desc");
while(resultset.next()){
rowCount++;
}
int firstrow = rowCount-10;
System.out.println(firstrow);
if(rowCount > 10){
resultset = statement.executeQuery("select * from textdata where Rowcount>'"+firstrow+"' order by Date desc, Time desc");
}else{
resultset = statement.executeQuery("select * from textdata order by Date desc, Time desc");
}
%>
<TABLE BORDER="1">
<TR>
<TH>Mach ID</TH>
<TH>Date</TH>
<TH>Time</TH>
<TH>Text1</TH>
<TH>Text2</TH>
<TH>Text3</TH>
<TH>Text4</TH>
<TH>Text5</TH>
<TH>Text6</TH>
<TH>Text7</TH>
<TH>Text8</TH>
<TH>Text9</TH>
<TH>Text10</TH>
<TH>Text11</TH>
<TH>Text12</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD> <%= resultset.getInt(1) %></td>
<TD> <%= resultset.getString(2) %></TD>
<TD> <%= resultset.getString(3) %></TD>
<TD> <%= resultset.getString(4) %></TD>
<TD> <%= resultset.getString(5) %></TD>
<TD> <%= resultset.getString(6) %></TD>
<TD> <%= resultset.getString(7) %></TD>
<TD> <%= resultset.getString(8) %></TD>
<TD> <%= resultset.getString(9) %></TD>
<TD> <%= resultset.getString(10) %></TD>
<TD> <%= resultset.getString(11) %></TD>
<TD> <%= resultset.getString(12) %></TD>
<TD> <%= resultset.getString(13) %></TD>
<TD> <%= resultset.getString(14) %></TD>
<TD> <%= resultset.getString(15) %></TD>
</TR>
<% } %>
</TABLE>
</div>
</br>
<form name = "Field_Details" action = "ServletApp" method= "get">
<fieldset style="float: center; width:900px; height: 75px;background-color:ivory; border-color:black;">
<font size = "2">Output Field :</font> <input type="text" name="Text1" maxlength="50" style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2"> MachId :</font> <input type="text" name="Text2" maxlength="15" style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2"> From Date(dd/mm/yy) :</font> <input type="text" name="Text3" maxlength="8" style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2"> To Date(dd/mm/yy) :</font> <input type="text" name="Text4" maxlength="8" style="height:15px; width:100px; border-color:black"><font size = "2"></font><br><br>
<input type= "submit" value="Send" style="height:30px; width:80px; margin-left:15px">
<input type= "submit" value="Search" style="height:30px; width:80px; margin-left:700px" onclick="form.action='FirstServlet';">
</BODY>
</HTML>
JSP is a server side technology, which means that if you want to refresh the page you will have to perform a request to the server which will return the new page. It is not possible to just return part of a page through normal JSP mechanisms.
If you want to just refresh the table you will need to use javascript to make an ajax call to the server to get the data you need, and repopulate the table with this data.
I altered code as below in my NewFile.jsp
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var auto = setInterval( function refresh()
{
$("#result").load("DemoFile.jsp");
}, 5000); // refresh every 5000 milliseconds
refresh();
</script>
then created new jsp file named DemoFile.jsp and copied part of code of NewFile.jsp division which I wanted to refresh.
In this portion of the code:
try {
connection = DriverManager.getConnection(
connectionUrl + dbName, userId, password);
statement = connection.createStatement();
String sql = "SELECT text_pub, path_photo FROM publication,publier, consomateur WHERE publication.id_publication=publier.publication_id_publication AND publier.users_id_user=consomateur.code_bar AND consomateur.code_bar = "+idUser+" AND 'path_photo' IS NOT NULL AND 'text_pub' IS NOT NULL";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
%>
<tr bgcolor="#8FBC8F">
<td><%=resultSet.getString("text_pub")%></td>
<img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130"></td>
<%}
%>
I'm trying to select two values from my table, some of the values can be null, for example I can have a value for my column text_pub and not have a value in my column path_photo and vice versa, so in case I have text_pub and don't have path_photo, I want to display only the text_pub, but the probleme is that the value of text_pub is displayed but also the value of path_photo which is null.
PS: I know that treatements like these are better done in servlets, but I really want to make it work in a jsp. Thank you.
Either add a IFNULL check to the sql query:
SELECT IFNULL(text_pub,DEFAULT("")), IFNULL(path_photo,DEFAULT("")) FROM publication,publier,...
or add a check for the return value of resultSet.getString(..)
String text = resultSet.getString("text_pub");
if (text==null) {
text = "";
}
Edit:
in case I have text_pub and don't have path_photo, I want to display
only the text_pub this answer doesn't cover this
<%
while (resultSet.next()) {
String pub = resultSet.getString("text_pub");
if (pub==null) {
pub = "";
}
String photo = resultSet.getString("path_photo");
%>
<tr bgcolor="#8FBC8F">
<td><%= pub %></td>
<%
if (photo!=null) {
%>
<td><img src="images/<%=photo%>" width="150" height="130"></td>
<%
}
}
%>
I know that treatements like these are better done in servlets, but I really want to make it work in a jsp.
You're wrong here. This kind of things are handled in JSP, not in servlet, since logic to present the contents in the view belongs to the View (JSP) and not to the Controller (Servlet). Since you're using scriptlets, you should use an if sentence to validate if you should display the image or not:
while (resultSet.next()) {
%>
<tr bgcolor="#8FBC8F">
<td><%=resultSet.getString("text_pub")%></td>
<td>
<%
if (resultSet.getString("path_photo") != null) {
%>
<img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130">
<%
}
%>
</td>
</tr>
<%
}
%>
If you don't want to display anything if resultSet.getString("text_pub") is null:
while (resultSet.next()) {
if (resultSet.getString("text_pub") != null) {
%>
<tr bgcolor="#8FBC8F">
<td><%=resultSet.getString("text_pub")%></td>
<td>
<%
if (resultSet.getString("path_photo") != null) {
%>
<img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130">
<%
}
%>
</td>
</tr>
<%
}
}
%>
If you happen to write the code in the right way and use a Servlet to handle the data retrieval from database, fill the data in a List<SomeEntity>, add this list into a request attribute (probably with name "photoList") and forward to the desired JSP, then you could use JSTL to control the flow of the image display:
<c:forEach items="${photoList}" var="photo">
<tr bgcolor="#8FBC8F">
<td>${photo.textPub}</td>
<td>
<c:if test="${not empty photo.pathPhoto}">
<img src="images/${photo.pathPhoto}" width="150" height="130">
</c:if>
</td>
</tr>
</c:forEach>
i fetched and displayed data as a table in html from mysql db using jsp and java now what i want is when a user clicks on the particular row then the data in that row should populate in 3 different tags
example if my table has this data
Name Place Mobile number
a abc 123
b def 234
c ghi 345
(The above table is fetched from mysql db)
if the user clicks on the 3rd Row then the data such as name place and mobile number should be displayed in 3 different tags as shown below
Name: c
Place: ghi
Mobile: 345
thanks in advance
Before i used to have a button on the right side of each row with the "Name"(if it is a row of c then the button has c) on it so i dressed the button by a pic using CSS.
here goes the code i used
<form action="Options2" method="post">
<table id="sorter" class="sortable" id="example" class="pretty">
<tr>
<th>Book Id</th>
<th>Title</th>
<th>Author</th>
<th>Category</th>
<th>Status</th>
<th>Owner</th>
<th>Borrow Date</th>
<th>Return Date</th>
<th>Requested By</th>
<th>Actions</th>
</tr>
<%
ArrayList rs=(ArrayList)request.getAttribute("news");
ListIterator itr=rs.listIterator();
int i=1;
while( itr.hasNext()){
%>
<tr>
<td><%=itr.next()%></td>
<% int Id = itr.nextIndex(); %>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<% int Id2 = itr.nextIndex(); %>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<%
String Bname=rs.get(Id).toString();
System.out.println(Bname);
String Stat=rs.get(Id2).toString();
System.out.println(Stat);
if(!Stat.equals("Not Availible"))
{
%>
<td>
<input class="buttonir" type="Submit" name="X" value="<%=Bname %>"></td>
</tr>
<%
}
}
%>
</table>
</form>
Try this:
$('table tr').click(function () {
var BookId = $(this).children('td:eq(0)').html();
var Title = $(this).children('td:eq(1)').html();
var Author = $(this).children('td:eq(2)').html();
$('div').html(
'Book Id: ' + BookId + '<br />' +
'Title: ' + Title + '<br />' +
'Author:' + Author + '<br />'
);
});
Demo: http://jsfiddle.net/UPxB9/1/
Compare it with your Code its almost same just few changes mentioned
Only following function is added in your code and two lines more which are highlighted out of code
Edit
Add following function in your (already working) javascript tag on this page or in a js file which you are using in this page
function displayRowData(yourRow)
{
var dv=document.getElementById('yourDivId');
dv.innerHTML="<br>Name : "+ yourRow.children[0].innerHTML";
dv.innerHTML += "<br>Place: "+yourRow.children[1].innerHTML";
dv.innerHTML += "<br>Name : "+yourRow.children[2].innerHTML";
}
<form action="Options2" method="post">
<table id="sorter" class="sortable" id="example" class="pretty">
<tr>
<th>Book Id</th>
<th>Title</th>
<th>Author</th>
<th>Category</th>
<th>Status</th>
<th>Owner</th>
<th>Borrow Date</th>
<th>Return Date</th>
<th>Requested By</th>
<th>Actions</th>
</tr>
<%
ArrayList rs=(ArrayList)request.getAttribute("news");
ListIterator itr=rs.listIterator();
int i=1;
while( itr.hasNext()){
%>
Following Line is just modified it was already in your code
<tr onclick='displayRowData(this)'>
<td><%=itr.next()%></td>
<% int Id = itr.nextIndex(); %>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<% int Id2 = itr.nextIndex(); %>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<td><%=itr.next()%></td>
<%
String Bname=rs.get(Id).toString();
System.out.println(Bname);
String Stat=rs.get(Id2).toString();
System.out.println(Stat);
if(!Stat.equals("Not Availible"))
{
%>
<td>
<input class="buttonir" type="Submit" name="X" value="<%=Bname %>"></td>
</tr>
<%
}
}
%>
</table>
Following line is added to your code
<div id='yourDivId'></div>
</form>