简体   繁体   English

如何在 JSP 页面中获取任何字符串值

[英]How can i fetch any string value in JSP page

If any "ID" set with value in Servlet Class then How can i access that value on my jsp page.如果在 Servlet 类中设置了任何带有值的“ID”,那么我如何在我的 jsp 页面上访问该值。 As i know there are several methods which helps to fetch the value by using Session management but i am tired of do it.据我所知,有几种方法可以通过使用会话管理来帮助获取值,但我厌倦了这样做。

Please help me out.请帮帮我。

Thanks in advance.提前致谢。

Typically the servlet calls HttpServletRequest.setAttribute to place a key value pair in the request.通常,servlet 调用 HttpServletRequest.setAttribute 以在请求中放置一个键值对。 Then the jsp page can access this value thru the 'request' jsp variable.然后jsp 页面可以通过'request' jsp 变量访问这个值。

From the servlet, set the file name in request context:从 servlet,在请求上下文中设置文件名:

request.setAttribute("filename", fileNameStr );

In JSP write:在 JSP 中写:

<%=request.getAttribute("filename")%>

Very simple via session, and you don't need to deal with passing parameters between pages.通过 session 非常简单,不需要处理页面之间的参数传递。

File 1:

String strVal = "one";
session.setAttribute("strVal",strVal);

File 2:

String strVal = (String)session.getAttribute("strVal");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM