eatSleepCode
eatSleepCode

Reputation: 4637

Session attribute returns null in Jsp

I am working on JSP and getting null value in session, here is my code

LoginServlet.java
        String userId = request.getParameter("userId");
        String password = request.getParameter("pwd");
        if(userId.equals("test") && password.equals("test"))
        {
            HttpSession session = request.getSession();
            session.setAttribute("user", userId); //session is set
            try 
            {
                response.sendRedirect("LoginSuccess.jsp");
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }

and in LoginSuccess.jsp I am retriving it like

Logged in user  = <% session.getAttribute("user");%>

but it returns null, not getting the reason why?

Upvotes: 0

Views: 8281

Answers (3)

user4527071
user4527071

Reputation: 1

String user = <% (String)session.getAttribute("user");%>

Upvotes: 0

Mitul Maheshwari
Mitul Maheshwari

Reputation: 2647

you don't need to create a instance of session in JSP. JSP is providing you inbuilt objects of all :- session , out, request , response..etc

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

It will give you user value, and also make sure that when you are setting user value in session session.setAttribute("user", userId); //session is set it is not null. just sysout that value.

Upvotes: 2

hurtbox
hurtbox

Reputation: 386

I might be wrong; but, in your LoginSuccess.jsp file, try changing the request.getParamater("UserId"); to "userId" with a lower-case u. In your servlet, you defined it with a lower-case. Make sure the names match

Upvotes: 0

Related Questions