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.
Related
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/
I wrote a jsp file for the web.And I use tomcat 8.5.
However, the table didn't come out as I want it to be.
the part came out right but the part is totally empty even no frames.
Here is the code below.
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%# page import="java.sql.*,enrollBean.*, Attendance.*"%>
<!DOCTYPE html>
<html><head><title> </title></head>
<body>
<%# include file="top_project.jsp"%>
<% if (session_id == null) response.sendRedirect("login.jsp"); %>
String c_id= request.getParameter("c_id");
<table width="75%" align="center" border> <br>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr>
<br>
<jsp:useBean id="att" class="AttendanceBean.AttendanceBean"/>
<jsp:useBean id="enrollMgr" class="enrollBean.EnrollMgr" />
<%
Vector vlist = enrollMgr.getEnrollList(session_id);
int counter = vlist.size();
Vector vlist2 = att.getAttendanceList(session_id,<%=en.getCId()%>);
int counter = vlist2.size();
%>
<tr>
<td align="center"><%=en.getCId()%></td>
<td align="center"><%=en.getCIdNo()%></td>
<td align="center"><%=en.getCName()%></td>
<td align="center"><%=en.getCUnit()%></td>
<td align="center"><%=en.getCId()%></td>
<td align="center"><%=en.getCIdNo()%></td>
<td align="center"><%=en.getCName()%></td>
<td align="center"><%=en.getCUnit()%></td>
</tr>
</table>
</body>
</html>
Please help me with fixing this error.
At least you have the following errors in JSP / HTML source:
String c_id= request.getParameter("c_id"); should be enclosed with <% ... %>.
<br> is not allowed between <table> and <tr>.
<br> is not allowed between </tr> and <tr>.
Is the variable en introduced in top_project.jsp?
I am trying to get a one-page input value to another page while clicking the order button.
while I will take a number of item value and click the order button, it will carry the value the order page. The page code is here,
<td><%=rs.getString("product_price")%></td>
<td> <input type="number" name="no_item" value="1" /></td>
<td class="text-center" width="250">
Order
Edit
Delete
</td>
The order page code is here,
<%
statement = connection.createStatement();
String u=request.getParameter("u");
String item_num =request.getParameter("no_item");
int num=Integer.parseInt(u);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
String product_price;
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<h4 style="float:left; padding-right:8px;">Product Name:</h4> <h4> <%=rs.getString("product_name")%> </h4>
</div>
<div class="form-group">
<%
product_price = rs.getString("product_price"); int num1 = Integer.parseInt(product_price); %>
</div>
<%= item_num %>
<%= num1 %>
<%
}
%>
Onclick of order button add the input value in url query string. you have need to use java script in jsp page.
<script>
function order(page, id){
input_value = document.getElementById('no_item').value;
location.href= page+"?u="+id+"&no_item="+input_value;
}
</script>
Add the onclick function within order button.
<a onclick="order('order.jsp', '<%=rs.getString("id")%>')" class="btn btn-success">Order</a>
In order.jsp page you will get the input value.
request.getParameter("no_item");
How to use underscore java template library for using template in java.
Library : https://github.com/javadev/underscore-java
Sample Template:
<h2>
<%- listTitle %>
</h2>
<ul>
<% _.each( listItems, function( listItem ){ %>
<li>
<%- listItem.name %>
<% if ( listItem.hasOlympicGold ){ %>
<em>*</em>
<% } %>
</li>
<% }); %>
</ul>
<% var showFootnote = _.any(
_.pluck( listItems, "hasOlympicGold" )
); %>
<% if ( showFootnote ){ %>
<p style="font-size: 12px ;">
<em>* Olympic gold medalist</em>
</p>
<% } %>
How can populate this template in java.
Thanks.
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>