【问题标题】:jsp jstl servlet (database table has two rows but jsp displays last row twice)jsp jstl servlet(数据库表有两行但jsp显示最后一行两次)
【发布时间】:2014-11-02 20:23:23
【问题描述】:

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

名为CompanyDB的DAO有这个方法

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

【问题讨论】:

    标签: jsp servlets jstl


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2015-04-13
      • 2012-06-22
      • 2013-02-23
      • 1970-01-01
      • 2011-12-24
      相关资源
      最近更新 更多