Redisplay submitted value in input field

2020-04-24 16:28发布

问题:

I have search functionality in my jsp page.. When user types "abc" and search for that. I get the result back but is there a way to retrieve that abc back to that search textbox along with the search results? Thank you.

回答1:

Just prefill the input value with the submitted value. Submitted input values are available in EL via the ${param} request parameter map keyed by input field names.

<input type="text" name="query" value="${fn:escapeXml(param.query)}" />

Note the importance of JSTL fn:escapeXml(). It's to prevent your page from XSS attacks while redisplaying user-controlled input.

The above example will prefill the input value with the result of request.getParameter("query"). As EL is null-safe, it won't display anything if it returns null.

See also:

  • XSS prevention in JSP/Servlet web application


回答2:

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

This will also work if you want something more basic, but I am not sure about the security



标签: jsp input