如何将字符串从Servlet传输到JSP?
servlet 文件
String str = req.getParameter("str");
req.setAttribute("str", "java");
getServletContext().getRequestDispatcher("/us.jsp").forward(req, resp);
jsp 文件
<jsp:useBean id="str" class="hws" scope="request">
或
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 EL(表达式语言,那些
${}
的东西)。它仅通过其属性名称即可隐式访问请求/会话/应用程序范围的属性。但无论何时涉及用户控制的输入,都要小心 XSS。
另请参阅:
Use EL (Expression Language, those
${}
things). It has implicit access to request/session/application scoped attributes by just its attribute name.Be careful with XSS though whenever it concerns user-controlled input.
See also:
虽然 BalusC 是正确的,但我想指出直接输出字符串的潜在安全风险。根据 Java Servlet 2.0 规范,
例如:
这可以帮助防止 XSS 攻击。有关详细信息,请参阅 OWASP 页面。
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,
For example:
This can help protected against XSS attacks. See the OWASP page for more info.