在同一页面上获取,处理和显示结果

时间:2012-06-24 13:44:27

标签: java jsp servlets javabeans

我有一个jsp页面,其中包含用户填写某些值的表单。现在我将这些值传递给一个servlet,它处理它并重新获取一组相关的数据。现在,我能够在servlet页面中检索数据,但是无法在我获取请求的同一个jsp页面上显示它。


我不想在jsp中使用java代码。我希望只使用servlet来做同样的事情。我不想做这样的事情


<% if(rollno==null)
      // then display the form
   else 
      // process the request on the jsp page itself  
%>

我想在servlet文件中处理我的所有结果,然后通过将数据从servlet传递到jsp 来显示jsp页面上的结果。我在下面发布我的代码:


<form id="form" method="post" action="searchServlet">
Student Roll No :<br><input type="text" value="" name="rollno"><br><br>
<input type="submit" value="SHOW DETAILS" />
</form>

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
            String rollno=request.getParameter("rollno");
            ResultSet rs=null;

            Class.forName("com.mysql.jdbc.Driver");  
            String url="jdbc:mysql://localhost/schooldatabase";
            Connection con = DriverManager.getConnection(url,"root","passwd");

            Statement st= (Statement) con.createStatement();
            String strquery="SELECT name,regno FROM `schooldatabase`.`student_info` WHERE rollno="+ rollno+ ";";

            if(!con.isClosed()) {
                rs=st.executeQuery(strquery);

                while(rs.next())
                {
                    out.println(rs.getString("name"));
                    out.println(rs.getString("regno"));
                }
            }

            else
            {    // The connection is closed 
            }
          }           

            catch(Exception e)
            {
                out.println(e);
            }

             finally {            
               out.close();
            }
}

1 个答案:

答案 0 :(得分:4)

您不应该在servlet中进行演示。您应该在servlet中

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

// ...
                out.println(rs.getString("name"));
                out.println(rs.getString("regno"));

// ...

            out.println(e);

您应该将数据存储在合理的集合中,并将其设置为请求属性,最后将请求/响应转发给JSP,然后JSP会围绕所有这些数据生成适当的HTML。像这样:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<Student> students = studentDAO.find(request.getParameter("rollno"));
        request.setAttribute("students", students); // Will be available as ${students} in JSP
        request.getRequestDispatcher("/WEB-INF/students.jsp").forward(request, response);
    } catch (SQLException e) {
        throw new ServletException("Cannot obtain students from DB", e);
    }
}

students.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${students}" var="student">
        <tr>
            <td>${student.name}</td>
            <td>${student.regno}</td>
        </tr>
    </c:forEach>
</table>

有关更具体的示例,请参阅此答案:Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern

另见: