Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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:

(String)session.getAttribute("name");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top