简体   繁体   English

使用JSTL设置会话变量并在servlet / controller类中访问它

[英]Set session variable using JSTL and accessing it in servlet/controller class

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 / controller类中访问变量“ 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类中获得它。

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 您可以使用JSTL设置session

<c:set var="para" value="valueHere" scope="session"  />

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>

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

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