Rahul Raj
Rahul Raj

Reputation: 3439

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.

Upvotes: 1

Views: 15611

Answers (2)

Under_stand
Under_stand

Reputation: 69

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>

Upvotes: 0

Afsun Khammadli
Afsun Khammadli

Reputation: 2068

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

Upvotes: 4

Related Questions