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?
Related
This is my arraylist for index 17
In this code may have data list or may not. Data is dispay when there are records. Not handled for no records
Java code:
//To avoid arrayindexoutofbound exception length checked
if (resultList.length >17) {
statisticsDetails.setqNames(resultList[17]);
//System.out.println(resultList[17]);
String[] qName=resultList[17].split("\\^");
List<StatisticsDetails> statisticsDetailsList = new ArrayList<StatisticsDetails>();
for (String queueName:qName ){
StatisticsDetails details = new StatisticsDetails();
String[] splitQueues = queueName.split("=");
details.setqKey(splitQueues[0]);
details.setqCount(splitQueues[1]);
statisticsDetailsList.add(details);
}
statisticsDetails.setqNamesList(statisticsDetailsList);
}
Jsp code:
<logic:iterate id="iteratorId" name="statistics.statisticsDetails" property="qNamesList">
<tr>
<td class="col-sm-6 col-md-6 col-lg-6"><bean:write name="iteratorId" property="qKey" /></td>
<td class="col-sm-6 col-md-6 col-lg-6"><bean:write name="iteratorId" property="qCount" /></td>
</tr>
</logic:iterate>
how to avoid arraybound error and JSP to handle when no data found
Use a null check using JSTL(Do not forget to import the tag library):
import core library:
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
and then use:
<c:if test="${not empty statistics.statisticsDetails}">
//keep your code here to iterate the list
</c:if>
Or use:
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
and then:
<c:if test="${fn:length(statistics.statisticsDetails) > 0}">
//keep your code here to iterate the list
</c:if>
Please check all mapping is correct or not in the struts-config.xml. In my case, i found incorrect type was mentioned for form and then it was giving the error. Type means package name for the form. in tag.
How can I get the average salary of all employees in the selected company?
I first select the company and then pass the id and based on that id, i get all employees in there and display their info in a table. The goal is to get the average salary of everyone in this group.
<%#page import="data.Employee"%>
<%#page import="data.Company"%>
<%#page import="java.util.List"%>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="company" type="data.Company" scope="request"/>
<% List<Employee> employees = company.getEmployees();
double sum=0.0;
%>
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Salary</td>
</tr>
<%
for(int i=0; i <employees.size(); i++){
sum += employees.get(i).getSalary();
%>
<tr>
<td><%=employees.get(i).getId()%></td>
<td><%=employees.get(i).getNom()%></td>
<td><%=employees.get(i).getSalary()%></td>
</tr>
<% } %>
</table>
//get the average salary of all employees ::: This is working based on JChris's answer
<p>Average salary of all employees in this company:<%=sum/(double)employees.size()%> </p>
//this is returning zero.
<p> New average method: <%=company.getAverageSalary()%></p>
You could sum the salaries up and then divide by the number of employees:
<% List<Employee> employees = company.getEmployees();
double sum=0.0;
%>
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Salary</td>
</tr>
<%
for(int i=0; i <employees.size(); i++){
sum += employees.get(i).getSalary();
%>
<tr>
<td><%=employees.get(i).getId()%></td>
<td><%=employees.get(i).getNom()%></td>
<td><%=employees.get(i).getSalary()%></td>
</tr>
<% } %>
</table>
//get the average salary of all employees
<p>Average salary of all employees in this company:<%=sum/(double)employees.size()%> </p>
Having said that, I urge you to reconsider before going down that road. Having code inside your JSP will severely hinder your chances of maintaining it if it gets any bigger than 1-2 pages.
All your business logic should happen in your code (e.g. inside your servlets) and in JSPs only display the information.
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.
I define a class of Clue :
Class Clue{
...
String text;
..
getter/setter method
}
I want to get ${clue.text} from jsp to use in the scope "<%...%>" to just show subSequence(0, 10),like this:
<c:forEach items="${clues}" var = "clue">
<tr>
<td>${clue.weibo.user.name}</td>
<td>
<%
String str = ${clue.weibo.text};
%>
<%=str.subSequence(0, 10) %>
</td>
</tr>
</c:forEach>
How can I do?
To access A JSTL variable in a scriplet:
<%
String str = (String)pageContext.getAttribute("clue.weibo.text", PageContext.REQUEST_SCOPE);
%>
However, a better way is to use JSTL functions to manipulate the string.
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:forEach items="${clues}" var = "clue">
<tr>
<td>${clue.weibo.user.name}</td>
<td>
${fn:substring(clue.weibo.text, 0, 10)}
</td>
</tr>
</c:forEach>
I'm trying to use a table to display some images, I'm allowed 3 images per row but I'm not sure how to end the table row when I have a row with less then 3 images. See the snippet below for what I've been trying.
Using a ul I would not have to worry about it but I'm directed to use a table. How can I make sure the last row is properly terminated ? I'm newer to jstl.
Thanks,
<% int endTRFlag = 0; %>
<table width="100%">
<tr>
<core:forEach var="imageURL" items="${actionBean.imageURLs}" varStatus="rowCounter">
<td align="center" valign="middle">
<div class="item">
<img src="${imageURL}" alt="Photo 1" class="img" />
</div>
</td>
<% endTRFlag = 1; %>
<core:if test="${ (rowCounter.count % 3 == 0) }">
</tr>
<tr>
<% endTRFlag = 0; %>
</core:if>
<core:if test="${ (rowCounter.count % 3 != 0) && (endTRFlag == 0)}">
</tr>
</core:if>
</core:forEach>
In the code below, I use .index instead of .count because it is 0-based, and so will work better for modulus (0/3, 3/3, etc.). .last is true if the current element is the last element in the list. This solution does cause validation errors in Eclipse (at least Ganymede), which is annoying.
<c:forEach var="imageURL" items="${actionBean.imageURLs}" varStatus="rowCounter">
<c:if test="${rowCounter.index mod 3 eq 0}">
<tr>
</c:if>
<td>stuff</td>
<c:if test="${(rowCounter.index+1) mod 3 eq 0 or rowCounter.last}">
</tr>
</c:if>
</c:forEach>
Just put the </tr> after the loop.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:set var="imageCnt" value="${fn:length(actionBean.imageURLs)}"/>
<table width="100%">
<tr>
<c:forEach var="imageURL" items="${actionBean.imageURLs}"
varStatus="rowCounter">
<td align="center" valign="middle">
<span class="item">
<img src="${imageURL}" alt="Photo 1" class="img" />
</span>
</td>
</c:forEach>
<c:forEach begin="${imageCnt}" end="2">
<td/>
</c:forEach>
</tr>
</table>