开发者

From Servlet to JSP

When trying to pass a table built with HTML in my servlet like that:

response.setContentType("text/html" );  
PrintWriter out = response.getWriter();  
out.println("<html>" );  
out.println("<head>" );  
out.println("<title>Imput OPC</title>" );
out.println("</head>" );  
out.println("<body>" ); 
...

and then

response.sendRedirect("/xxx.jsp" );

But I did not found any table in the JSP.

A friend told me to use a Bean but how can i catch values from the form ( because I have a treatement with the form before constructing table)in a bean. I must use a servlet for that. So what I want is exactly to construct in the response a table then send it to jsp knowing that: .sendRedirect and

开发者_如何学运维getServletContext().getRequestDispatcher("/xxx.jsp").forward(request, response); 

gives nothing at all.


A redirect let the client fire a new request. It trashes the current request and response you're working on. You get a fresh new request and response on the specified URL. You don't want to send a redirect whenever you want to pass request scoped information from servlet to JSP. Use a forward instead.

Printing HTML in a servlet is a big no-no. You should also not write something to the response body whenever you want to forward the request to a JSP later. You would face an IllegalStateException in the server logs (and indeed a blank page in the webbrowser). Printing HTML is a task which is to be done by JSP, not by servlet.

In a servlet you just need to do the business stuff. E.g. collecting information which is to be displayed in a table. First create a Javabean class which represents each item (row) of the table. Then create a DAO class which returns a list of those items from the datastore (a database?). Then in the servlet, just put the list of items in the request scope using HttpServletRequest#setAttribute(), forward the request to a JSP file using RequestDispatcher#forward() and iterate over the list of items using JSTL c:forEach tag (to install JSTL, just drop jstl-1.2.jar in /WEB-INF/lib).

Basic kickoff example:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Item> items = itemDAO.list();
    request.setAttribute("items", items); // It's now available as ${items} in EL.
    request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
}

where /WEB-INF/result.jsp look like this:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

...

<table>
    <c:forEach items="${items}" var="item">
        <tr>
            <td>${item.someProperty}</td>
            <td>${item.anotherProperty}</td>
        </tr>
    </c:forEach>
</table>

For more hints and examples you may find those tutorials useful. To go some steps further, you can also use a MVC framework so that you basically end up with only a Javabean class and a JSP file (i.e. the role of the servlet have been taken over by the MVC framework), for example JSF.


I'm not exactly sure what you want, but have you tried:

request.getRequestDispatcher("/xxx.jsp").forward(request, response);

The response.sendRedirect("/xxx.jsp" ) is just what it is, it tells the browser to go look somewhere else (http 301 with a location header of "../xxx.jsp"). No information is passed along in this redirect. That's what RequestDispatcher is for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜