views:

287

answers:

1

Is there any way to set request attributes (not parameters) when a form is posted?

The problem I am trying to solve is: I have a JSP page displaying some data in a couple of dropdown lists. When the form is posted, my Controller servlet processes this request (based on the parameters set/specified in the form) and redirects to the same JSP page that is supposed to display addition details. I now want to display the same/earlier data in the dropdown lists without having to recompute or recalculate to get that same data.

And in the said JSP page, the dropdown lists in the form are populated by data that is specified through request attributes. Right now, after the Form is POSTed and I am redirected to the same JSP page the dropdown lists are empty because the necessary request attributes are not present.

I am quite the n00b when it comes to web apps, so an obvious & easy solution to this problem escapes me at the moment!

I am open to suggestions on how to restructure the control flow in the Servlet.

Some details about this app: standard Servlet + JSP, JSTL, running in Apache Tomcat 6.0.

Thanks.

+1  A: 

.. and redirects to the same JSP page ..

You shouldn't fire a redirect here, but a forward. I.e. do not do

response.sendRedirect("page.jsp");

but rather do

request.getRequestDispatcher("page.jsp").forward(request, response);

This way the original request remains alive, including all the parameters and attributes. A redirect namely instructs the client to fire a brand new request, hereby garbaging the initial request.

In JSP you can access request parameters by ${param} in EL and you can access request attributes the same way with ${attributeKey} where attributeKey is the attribute key which you've used to set the object in the request scope in the servlet as follows:

request.setAttribute("attributeKey", someObject);

As to retaining HTML input values in a JSP, you just need to set the <input> element's value attribtue accordingly with the request parameter value:

<input name="foo" value="${param.foo}">

This prints the outcome of request.getParameter("foo") in template text. This has however a XSS risk, better is to escape any user-controlled input with help of JSTL's fn:escapeXml() as follows:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<input name="foo" value="${fn:escapeXml(param.foo)}">

Retaining the selected option in a dropdown is a bit different story. You basically need to set the selected attribute of the <option> element in question. Assuming that you're -as one usually would do- using JSTL's <c:forEach> tag to display a Map<String, String> or maybe a List<JavaBean> of option values, you can solve it as follows (assuming ${countries} is a Map<String, String> which you've placed as an attribute in the request, session or application scope):

<select name="country">
    <c:forEach items="${countries}" var="country">
        <option value="${country.key}" ${country.key == param.country ? 'selected' : ''}>${country.value}</option>
    </c:forEach>
</select>

This prints the selected attribute when the currently iterated option key equals the submitted one in the request parameter map.

BalusC
I forgot to mention that I am already doing a `rd.forward(request, response)`. But what's confounding me right now is that after the forward() the JSP's dropdown lists are empty - and I can see in Netbeans' HTTP monitor that the request attributes that contained the data used to populate the dropdown lists are no longer present after the forward() completes. I am missing sth!
ssahmed555
Huh, HTTP monitor? Aren't you confusing request attributes with request parameters? Attributes are not visible in HTTP request/response header/body, but only in server's memory. Request parameters are visible in HTTP request/response header/body. You'll really need to provide more detail about the problem. Regardless, didn't the remnant of my answer bring new insights?
BalusC
ssahmed555
I don't use Netbeans, but I find it hard to believe, because the term "HTTP" in the "HTTP monitor" would then be misplaced. Request attributes are absolutely not part of HTTP traffic. After all, the symptoms indicate that you're either doing a redirect or accessing the attributes/parameters the wrong way.
BalusC