java 使用 JSTL 设置会话变量并在 servlet/控制器类中访问它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30526726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set session variable using JSTL and accessing it in servlet/controller class
提问by Rahul Raj
If I set session variable using JSTL like this:
如果我像这样使用 JSTL 设置会话变量:
<c:set var="para" value="${CLIENT_LOGO}" scope="session" />
Then how can I access the variable "para" in a servlet/controller class?
那么如何访问 servlet/控制器类中的变量“para”?
I tried the below code variations, but none worked.
我尝试了下面的代码变体,但没有一个奏效。
request.getAtrribute("para")
request.getSession().getAtrribute("para")
Note: I'm not looking for a solution to print the value in a jsp something like:
注意:我不是在寻找在 jsp 中打印值的解决方案,例如:
<c:out value="${sessionScope.para}" />
But instead, I would like to know whether any solution possible to get it in Java class.
但是相反,我想知道是否有任何解决方案可以在 Java 类中获取它。
回答by Afsun Khammadli
You have to do following code in your servlet:
您必须在 servlet 中执行以下代码:
HttpSession session = request.getSession();
String para = session.getAttribute("para");
You can set session
with JSTL
您可以设置session
与JSTL
<c:set var="para" value="valueHere" scope="session" />
回答by Under_stand
One can solve this problem by referring to this code.
可以参考这段代码来解决这个问题。
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
This JSP stores the ultimate answer in a session-scoped variable where
the other JSPs in the web application can access it.
<p />
<c:set var="theUltimateAnswer" value="${41+1}" scope="session" />
Click <a href="displayAttributes.jsp">here</a> to view it.
</body>
</html>
For displaying value
用于显示值
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Retrieval of attributes</title>
</head>
<body>
The ultimate answer is <c:out value="${sessionScope.theUltimateAnswer}" /> <br/>
</body>
</html>