简体   繁体   中英

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

If I set session variable using JSTL like this:

<c:set var="para" value="${CLIENT_LOGO}" scope="session"  />

Then how can I access the variable "para" in a servlet/controller class?

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:

<c:out value="${sessionScope.para}" />

But instead, I would like to know whether any solution possible to get it in Java class.

You have to do following code in your servlet:

HttpSession session = request.getSession();
String para = session.getAttribute("para");

You can set session with JSTL

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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