jsp jstl servlet(数据库表有两行,但jsp显示最后一行两次)

时间:2014-11-02 20:23:23

标签: jsp servlets jstl

问题:数据库表有两行。但是当我在jsp中显示它时 显示最后一行两次而不是每一行..请帮助!!!

DAO名为CompanyDB有此方法

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;
    }
}

我的jsp页面看起来像这样

<%@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" %>

我的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();
        }

1 个答案:

答案 0 :(得分:1)

您只需创建一个 PostJob实例,并在集合中多次添加此唯一实例,并在每次迭代时更改其值。每次迭代都应该调用new PostJob()

while(rs.next()){
    PostJob p = new PostJob();
    ...
    viewList.add(p);
}