Reputation: 3
problem: database table has two rows. But when i display in jsp it shows last row twice instead of each individual row..Please help!!!
DAO named CompanyDB has this method
public List viewPostedJobs(){
ArrayList<PostJob> viewList = new ArrayList<PostJob>();
PostJob p = new PostJob();
String sql="select * from postjob";
try{
connection=getConnection();
s=connection.createStatement();
rs=s.executeQuery(sql);
while(rs.next()){
p.setLocation(rs.getString("location"));
p.setSkills(rs.getString("skills"));
p.setField(rs.getString("field"));
p.setDescription(rs.getString("description"));
p.setEmail(rs.getString("email"));
p.setCompanyName(rs.getString("companyName"));
p.setQualification(rs.getString("qualification"));
viewList.add(p);
}
}catch(Exception asd){
System.out.println(asd.getMessage());
}
/*catch (SQLException ex) {
Logger.getLogger(CompanyDB.class.getName()).log(Level.SEVERE, null, ex);
} */
return viewList;
}
}
MY jsp page looks like this
<%@include file="head.html" %>
<%@page import="Model.PostJob" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div class="profile">
<div style="top: 233px; left: 172px;" class="sidebar2">
<form class="center"> <input class="form-submit-button" value="Company Profile" type="submit"> </form>
<br>
<form class="center" action="post_job.html"> <input class="form-submit-button" value="Post New Job" type="submit"> </form>
<br>
<form class="center" action="view_posted_job.html"> <input class="form-submit-button" value="View Posted Job" type="submit"> </form>
<br>
<form class="center"> <input class="form-submit-button" value="Check Mail" type="submit"> </form>
<br>
<form class="center" action="change_pass.html"> <input class="form-submit-button" value="Change Password" type="submit"> </form>
<br>
<form class="center"> <input class="form-submit-button" value="Logout" type="submit"> </form>
<br>
</div>
<div class="jumbotron2">
<h2 align="center">Posted Jobs</h2>
<p>
<div>
<table border="5px" align="center">
<tr>
<th>Location</th>
<th>Skills</th>
<th>Field</th>
<th>Description</th>
<th>Email</th>
<th>Company</th>
<th>Qualification</th>
</tr>
<c:forEach items="${PostJob}" var="p" varStatus="varCounter">
<tr><td><c:out value="${p.location}" /></td>
<td><c:out value="${p.skills}" /></td>
<td><c:out value="${p.field}" /></td>
<td><c:out value="${p.description}" /></td>
<td><c:out value="${p.email}" /></td>
<td><c:out value="${p.companyName}" /></td>
<td><c:out value="${p.qualification}" /></td></tr>
</c:forEach>
</table>
</div>
</p>
</div>
</div>
</body>
</html>
<%@include file="foot.html" %>
my Servlet
public class viewJobServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
List<PostJob> viewJob=new ArrayList<PostJob>();
CompanyDB cdb = new CompanyDB();
viewJob = cdb.viewPostedJobs();
request.setAttribute("PostJob", viewJob);
//response.sendRedirect("viewPostedJobs.jsp");
RequestDispatcher view=request.getRequestDispatcher("viewPostedJobs.jsp");
if(view !=null){
view.forward(request, response);
}
}finally{
out.close();
}
Upvotes: 0
Views: 561
Reputation: 691933
You're only creating one PostJob
instance, and adding this unique instance several times in the collection, changing its value at each iteration. The call to new PostJob()
should be made at each iteration:
while(rs.next()){
PostJob p = new PostJob();
...
viewList.add(p);
}
Upvotes: 1