Access servlet declared HttpSession in jsp

If the username, password and location are correct in login servlet i am creating a HttpSession and get to the jsp page. Below is the code in servlet:

    HttpSession sessionsa = request.getSession(true);
    sessionsa.setAttribute("user",userName); //userName is a String variable
    sessionsa.setAttribute("location",location); //location in value place is a String variable

Now on jsp page I am not able to access the attributes. Code on jsp:

    sessionsa = request.getSession();
    String user = (String) sessionsa.getAttribute("user");
    String location = (String) sessionsa.getAttribute("location");

It states that cannot find symbol variable sessionsa in class SimplifiedJSPServlet. Please help. Have been googling it since 2 days.


Do like this,first make the session creation false in your jsp.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>

Then to get the session,

HttpSession sessionsa = request.getSession(false);
String user = (String) sessionsa.getAttribute("user");
String location = (String) sessionsa.getAttribute("location");

By this you will get the user and location from the session.Hope this will help you.

You can directly use session variable (HttpSession object associated with the request), it is available as Implicit Objects in JSP.You can use session object without any initialization or getSession().

session object is available in JSP if you haven't include below line in JSP

<%@ page session="false" %>  

It will disable session tracking in JSP file in which it is included.If this line is included in JSP you cant able to used session object in JSP directly.

From Rererence doc:

JSP Implicit Objects are the Java objects that the JSP Container makes available 
to developers in each page and developer can call them directly without being   
explicitly declared.

In your case you can used below code.

String user = (String) session.getAttribute("user");
String location = (String) session.getAttribute("location");  

You can also try to directly fetch values using session variable which is pre defined in scope of every jsp:


Need Your Help

Java add mouseListener and use local variables inside

java swing variables instance-variables mouselistener

What I'm trying to do is use local variables inside of the mouseListener I add ( right on that place ). Which seems impossible, so I would like to ask if there's any alternative way for what I'm tr...

how to update the next row value as same value of first row in jtable?

java swing jtable tablemodel

In Jtable, if I set the value of the first row second column to 20, then the value of the second row second column should be updated to 20 (same value) automatically.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.