Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set request attributes when a Form is POSTed

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.

like image 629
ssahmed555 Avatar asked Oct 15 '22 05:10

ssahmed555


1 Answers

.. 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.

like image 179
BalusC Avatar answered Nov 03 '22 22:11

BalusC