tags:

views:

61

answers:

2

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.

+1  A: 

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.

Gummyball
+2  A: 

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.

BalusC
Think you for the answer I will be more clear.What I want to realize is th following:In a JSP x I have a 3 dropdownlist and a button called edit when user click this button a table dynamically should be displayed now this table is modified correponding the values in those 3 ddl.So it can be 3 cols or 4 cols or even 6 cols it depends on the selected values.So what I tried is to use the servlet to getParameter doing the if clause construct the html and then forward the response.Have you any the suggestion of the structure that I can use.
kawtousse
I still confused untill now.Thinks a lot for the usefull help that you give to me.
kawtousse
You're welcome. If you have a completely new and independent question, press `Ask Question` button at the right top. Don't forget to mark answers of previous questions accepted.
BalusC