Reputation: 4637
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
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
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