Set request attributes when submitting a form

Is there a way to set request attributes (not parameters) when submitting a form?

The problem I'm trying to solve is this: I have a JSP page displaying some data in several drop down lists. When the form is submitted, my Controller servlet processes this request (based on the parameters specified in the form) and redirects to the same JSP page that should display additional data. Now I want to display the same / earlier data in the drop-down lists without having to recount or recount to get the same data.

And on the specified JSP page, the drop-down lists in the form are populated with the data specified using the request attributes. Right now, after the POSTED form and I are redirected to the same JSP page, the drop-down lists are empty because there are no required query attributes.

I'm pretty n00b when it comes to web applications, so the obvious and simple solution to this problem is eluding me at the moment!

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

Some information about this application: standard servlet + JSP, JSTL running on Apache Tomcat 6.0.

Thank.

+5
source share
1 answer

.. and redirected to the same JSP page ..

, .

response.sendRedirect("page.jsp");

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

, , . , , , .

JSP ${param} EL, ${attributeKey}, attributeKey - , :

request.setAttribute("attributeKey", someObject);

HTML JSP, <input> value :

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

request.getParameter("foo") . XSS, JSTL fn:escapeXml() :

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

- . selected <option>. , , , , JSTL <c:forEach> Map<String, String> , , List<JavaBean>, (, ${countries} Map<String, String> , ):

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

selected, , .

+4

All Articles