• Post Reply Bookmark Topic Watch Topic
  • New Topic

when i do sessionUser.invalidate() and try to login again then why it gives me error  RSS feed

 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
HERE IS THE COMPLETE CODE IF IS THERE SOMETHING WRONG THEN PLEASE CORRECT ME.

loginUser.jsp redirected from simple login page:

FileName:loginUser.jsp
*/


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="user.UserDetails,java.util.*,javax.servlet.http.*,java.io.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%!

UserDetails loginUser;
ArrayList <UserDetails> userList = new ArrayList<UserDetails>();

public void jspInit()
{
loginUser = new UserDetails(319,"Gajanan Chalke","Dighi Pune",7000.0f,"admin","admin@123",true);
userList.add(loginUser);
loginUser = new UserDetails(1,"abc","abc",5000.0f,"abc","abc",false);
userList.add(loginUser);
loginUser = new UserDetails(2,"pqr","pqr",4000.0f,"pqr","pqr",false);
userList.add(loginUser);
loginUser = new UserDetails(3,"lmn","lmn",3000.0f,"lmn","lmn",false);
userList.add(loginUser);
loginUser = new UserDetails(4,"narendra","kasaba peth pune",8000.0f,"naren","naren",true);
userList.add(loginUser);

}
%>
<form name="loginPage" method="post" action="loginUser.jsp">

<%
HttpSession sessionUser = request.getSession(true);
if(sessionUser.isNew())
{
String loginID = request.getParameter("loginId");
String loginPass = request.getParameter("loginPass");

loginUser = new UserDetails();

loginUser=loginUser.login(userList, loginID, loginPass);

if(loginUser==null)
{
response.getWriter().println("WRONG LOGIN-ID OR PASSWORD");
response.getWriter().println("<br><a href='http://localhost:8080/LoginUser/index.jsp' style='font-size:20px;'> Login Again </a><br><br><br>");
sessionUser.invalidate();

}
else
{
sessionUser.setAttribute("UserInfo", loginUser);
response.getWriter().println("<br>****LOGIN SUCCESSFUL*****\n\n\n");
response.getWriter().println("<br>"+sessionUser.getId());
if(loginUser.isAdmin())
{
out.println("<input name='add' type=submit value='Add'>");

}

out.println("<input name='show' type=submit value='Show'>");
out.println("<input name='logout' type=submit value='Logout'>");

}

}
else
{
loginUser=(UserDetails)sessionUser.getAttribute("UserInfo");
response.getWriter().println("<br>"+sessionUser.getId());
if(request.getParameter("add")!=null)
{

}
else if(request.getParameter("show")!=null)
{
out.println("<br><a href='http://localhost:8080/LoginUser/loginUser.jsp' style='font-size:20px;'> HOME </a><br><br><br>");

if(loginUser.isAdmin())
{

for(int i=0;i<userList.size();i++)
userList.get(i).getUserDetails(response.getWriter());

}
else
{
loginUser.getUserDetails(response.getWriter());
}


}
else if(request.getParameter("logout")!=null)
{
loginUser.logout(response.getWriter());
loginUser=null;
out.println("<br><a href='index.jsp' style='font-size:20px;'> Login Again </a><br><br><br>");
sessionUser.invalidate();

/*
sessionUser.setAttribute("UserInfo", null);
sessionUser = new HttpSession;
response.sendRedirect("index.jsp"); */

}
else
{
if(loginUser.isAdmin())
{
out.println("<input name='add' type=submit value='Add'>");

}

out.println("<input name='show' type=submit value='Show'>");
out.println("<input name='logout' type=submit value='Logout'>");
}


}

%>

</form>

</body>
</html>

 
E Armitage
Rancher
Posts: 989
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.) Don't write Java code in JSPs. Use JSPs for display logic only and put Java code in Java classes which you call from servlets.
2.) Don't write your own security module. Use security mechanisms provided for EE applications
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:1.) Don't write Java code in JSPs. Use JSPs for display logic only and put Java code in Java classes which you call from servlets.
2.) Don't write your own security module. Use security mechanisms provided for EE applications


actually i'm new to java and this is assignment given to me i did at my best. it works fine for first time when you login but goes fail when you logout and try to login again with new user id and password
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And they told you in the assignment that you must write Java code in JSPs?
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:And they told you in the assignment that you must write Java code in JSPs?


no the told me to implement it in two way one with the help of servlet and another only with jsp first one is done i've given the url to form and done url mapping to servlet in web.xml

but in another way it is not working so i need help to resolve this issue.
 
Anindya Roy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
failure is the success of pillar. You can do better coding only after correcting mistakes
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anindya Roy wrote:failure is the success of pillar. You can do better coding only after correcting mistakes


What's the wrong in my code? tell me I'll correct it!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GajanaN Chalke wrote:
Anindya Roy wrote:failure is the success of pillar. You can do better coding only after correcting mistakes


What's the wrong in my code? tell me I'll correct it!

E Armitage wrote:1.) Don't write Java code in JSPs. Use JSPs for display logic only and put Java code in Java classes which you call from servlets.
2.) Don't write your own security module. Use security mechanisms provided for EE applications
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GajanaN Chalke wrote:
Anindya Roy wrote:failure is the success of pillar. You can do better coding only after correcting mistakes


What's the wrong in my code? tell me I'll correct it!


If you have to persist with the code then you need to check the error message you are getting if any and trace where it's coming from. If you are getting an exception trace it will likely have line numbers pointing where the problem is.
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@E Armitage::

I told you it works fine for the first time it starts but goes fail when user logout and try to login with same or different ID/password

it shows following error::
--------------------------------------------------------------------
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /loginUser.jsp at line 114

111: }
112: else
113: {
114: if(loginUser.isAdmin())
115: {
116: out.println("<input name='add' type=submit value='Add'>");
117:


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause

java.lang.NullPointerException
org.apache.jsp.loginUser_jsp._jspService(loginUser_jsp.java:180)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.23
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does that mean?
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The part that says

java.lang.NullPointerException
org.apache.jsp.loginUser_jsp._jspService(loginUser_jsp.java:180)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)



means that somewhere around line 180 of the class created for your loginUser.jsp you are causing a nullpointer exception.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NPE on line 114 would indicate

the variable loginUser is null
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every one i got it!

instead of
if(sessionUser.isNew())

i used as following

if(sessionUser.getAttribute("UserInfo")==null)



and also added the null check at line no 114


and now the issue is resolved thank you all...
 
GajanaN Chalke
Greenhorn
Posts: 16
Linux Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone i've one doubt now i removed the null check from line no 114 but still it is working fine that means error was not there it is in

if(sessionUser.isNew())

so i just want to know that

if(sessionUser.getAttribute("UserInfo")==null)

what is the difference between this one and previous one?
because if new session is created that mean there is no any attribute right?
then what is wrong in "if(sessionUser.isNew()) "
this statement?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!