开发者

How to transfer String from Servlet to JSP?

开发者 https://www.devze.com 2023-01-30 13:34 出处:网络
servlet file String str = req.getParameter(\"str\"); req.setAttribute(\"str\", \"java\"); getServletContext().getRequestDispatcher(\"/us.jsp\").forward(req, resp);

servlet file

String str = req.getParameter("str");
req.setAttribute("str", "java");
getServletContext().getRequestDispatcher("/us.jsp").forward(req, resp);

jsp file

<jsp:useBean id="str" class="hws" scope="request">

or

<div align="center">
    <textarea readonly name="" cols="50" rows="25"><%= request.getAttribute("str") %></ textarea>
</div>
<form action="/us" method="post">
    <div align="center">
    开发者_高级运维    <textarea name="str" cols="50" rows="3">welcome to my program</textarea>
    </div>
</form>


Use EL (Expression Language, those ${} things). It has implicit access to request/session/application scoped attributes by just its attribute name.

<textarea readonly>${str}</textarea>

Be careful with XSS though whenever it concerns user-controlled input.

See also:

  • JSP tag info page
  • Servlets tag info page


While BalusC is correct, I wanted to point out the potential security risk with directly outputting a string. According to the Java Servlet 2.0 spec,

In cases where escaping is desired (for example, to help prevent cross-site scripting attacks), the JSTL core tag can be used.

For example:

<c:out value=”${anELexpression}” />

This can help protected against XSS attacks. See the OWASP page for more info.

0

精彩评论

暂无评论...
验证码 换一张
取 消