How to pass a string from a servlet to JSP?

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>
+3
source share
2 answers

Use EL (expression language, those ${}things). It has implicit access to application attributes associated with the request / session / application only by its attribute name.

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

Be careful with XSS , though whenever it comes to user login.

See also:

+4
source

While BalusC is correct, I would like to point out a potential security risk with direct line output. According to the Java Servlet 2.0 spec ,

, (, ), JSTL.

:

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

XSS. . OWASP.

+4

Source: https://habr.com/ru/post/1779671/


All Articles