Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>.. and redirects to the same JSP page ..</em></p> </blockquote> <p>You shouldn't fire a redirect here, but a <strong>forward</strong>. I.e. do not do</p> <pre><code>response.sendRedirect("page.jsp"); </code></pre> <p>but rather do</p> <pre><code>request.getRequestDispatcher("page.jsp").forward(request, response); </code></pre> <p>This way the original request remains alive, including all the parameters and attributes. A redirect namely instructs the client to fire a <strong>brand new</strong> request, hereby garbaging the initial request.</p> <p>In JSP you can access request parameters by <code>${param}</code> in EL and you can access request attributes the same way with <code>${attributeKey}</code> where <code>attributeKey</code> is the attribute key which you've used to set the object in the request scope in the servlet as follows:</p> <pre><code>request.setAttribute("attributeKey", someObject); </code></pre> <p>As to retaining HTML input values in a JSP, you just need to set the <code>&lt;input&gt;</code> element's <code>value</code> attribtue accordingly with the request parameter value:</p> <pre><code>&lt;input name="foo" value="${param.foo}"&gt; </code></pre> <p>This prints the outcome of <code>request.getParameter("foo")</code> in template text. This has however a <a href="http://en.wikipedia.org/wiki/Cross-site_scripting" rel="nofollow noreferrer">XSS</a> risk, better is to escape any user-controlled input with help of JSTL's <code>fn:escapeXml()</code> as follows:</p> <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %&gt; ... &lt;input name="foo" value="${fn:escapeXml(param.foo)}"&gt; </code></pre> <p>Retaining the selected option in a dropdown is a bit different story. You basically need to set the <code>selected</code> attribute of the <code>&lt;option&gt;</code> element in question. Assuming that you're -as one usually would do- using JSTL's <code>&lt;c:forEach&gt;</code> tag to display a <code>Map&lt;String, String&gt;</code> or maybe a <code>List&lt;JavaBean&gt;</code> of option values, you can solve it as follows (assuming <code>${countries}</code> is a <code>Map&lt;String, String&gt;</code> which you've placed as an attribute in the request, session or application scope):</p> <pre><code>&lt;select name="country"&gt; &lt;c:forEach items="${countries}" var="country"&gt; &lt;option value="${country.key}" ${country.key == param.country ? 'selected' : ''}&gt;${country.value}&lt;/option&gt; &lt;/c:forEach&gt; &lt;/select&gt; </code></pre> <p>This prints the <code>selected</code> attribute when the currently iterated option key equals the submitted one in the request parameter map.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload