Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Session Login And Logout

Posted on 2003-11-30
7
166,746 Views
Last Modified: 2011-08-18
Hi there,

I’m currently on an application that has an administrative module. I’m experiencing problems in clearing the sessions so as to log a user out and also to login as an admin and as a user of the application. I’m using a Java Bean for this purpose.


This is the code to check to see if there is a login session as an administrator or as a user. If there is no login session, the page will display a noAuthority.jsp ….. else it will show the page contents.

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<jsp:useBean class = "Beans.User" id = "userid" scope = "application"></jsp:useBean>
<jsp:setProperty name = "userid" property = "*"/>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<%if (session.isNew()==true)
response.sendRedirect(response.encodeRedirectURL("login.jsp"));%>

<%
boolean userAdmin = userid.checkUser();
if (userAdmin == false)
{%> <jsp:forward page = "noAuthority.jsp" /> <%}
else
{%>
<%
String loginName = userid.getUserName();
String loginPass = userid.getUserPass();
%>

Username: <% out.println(loginName); %> <br>
Password: <% out.println(loginPass); %> <br>
<h5> Hello, <%= userid.getUserName() %> You are Authorized! </h5> <br>
<b>Session ID: </b><%= session.getId() %><br>
<a href = "LogOut.jsp">logout test</a>

<%}%>

</body>
</html>


This is the Login_action page to process the login session with validation for username and password. The admin username is hardcoded and usernames are stored in a MySQL database.


<%if (session.isNew()==true)
response.sendRedirect(response.encodeRedirectURL("login.jsp"));%>

<!-- Validation Page for Login-->
<!-- Open connection and execute query -->
<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
Statement statement = connection.createStatement();
%>

<% ResultSet rs = statement.executeQuery("Select * from users"); %>
<%
boolean userValidate, passValidate;
userValidate = false;
passValidate = false;
String name = " ";

if (rs != null)
      {while (rs.next())
            {name = rs.getString("Username");
                        if (name.equals(request.getParameter("usernm")))
                              {userid.setUserName(name);
                              userValidate = true;
                              session.setAttribute("Pass-name", name);
                              session.setMaxInactiveInterval(60);
                               String pass = rs.getString("password");
                                    if (pass.equals(request.getParameter("pass")))
                                          {userid.setUserPass(pass);
                                          passValidate = true;}}}}

if (userValidate==true && passValidate == true)
      {response.sendRedirect(response.encodeRedirectURL("success.jsp"));}
            
else if (userValidate==true && passValidate == false)
{out.println("Password Error! Pls Try Again.");
%> <a href = "login.jsp"> BACK </a> <%}

else
{out.println("User Name Error! Pls Try Again.");}

%>




This is the logout page code. Basically clears the session created during the login.

<body>

<%if (session.isNew()==true)
response.sendRedirect(response.encodeRedirectURL("login.jsp"));%>

<%session.invalidate();%>
<h4> You were being Logged out </h4> <br>
<a href = "login.jsp"> Login </a><br>
<b>Session ID: </b><%= session.getId() %>
</body>


Currently, the problem that I’m facing is that when I logout of the application, I’m still able to access a page by typing the URL of the page into the browser twice. For example,
http://localhost:8080/newtemplate/create_user.jsp

Also, sometimes, there is an error page that shows the message; forward statement cannot proceed because a response has been committed.

Any help on this is greatly appreciated.
0
Comment
Question by:keneticintrouble
  • 4
  • 2
7 Comments
 

Author Comment

by:keneticintrouble
ID: 9847387
Additional Queries:

I'm using the Tomcat Web Server to host the JSP so is there such a thing as a tomcat cache that stores sessions?? If so, how can i clear that because i think my codes recognise that as a session instead of the admin module's session.

Thanks again
0
 
LVL 92

Accepted Solution

by:
objects earned 80 total points
ID: 9847580
> <%if (session.isNew()==true)

You need to do more than that, that only checks if session has just been created. So the 2nd time the page is loaded it will return false.
0
 
LVL 92

Expert Comment

by:objects
ID: 9847589
You need to check userid to see if it contains an authenticated user.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 9847600
> <jsp:useBean class = "Beans.User" id = "userid" scope = "application"></jsp:useBean>

That should also have session scope, and not application.
0
 
LVL 4

Assisted Solution

by:kokchoon78
kokchoon78 earned 70 total points
ID: 9854383
>>Also, sometimes, there is an error page that shows the message; forward statement cannot proceed because a response has been committed.

That is because the response has been committed after you try to redirect to another page :

Try to move your sendRedirect method before any html tags.

for example :

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<jsp:useBean class = "Beans.User" id = "userid" scope = "application"></jsp:useBean>
<jsp:setProperty name = "userid" property = "*"/>

<%if (session.isNew()==true)
response.sendRedirect(response.encodeRedirectURL("login.jsp"));%>

<%
boolean userAdmin = userid.checkUser();
if (userAdmin == false)
{%> <jsp:forward page = "noAuthority.jsp" /> <%}
else
{%>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
...

objects is correct, try to use the following to check the session :

if (session.getAttribute("Pass-name") != null )



0
 

Author Comment

by:keneticintrouble
ID: 9859627
Dear Participants,

All your help has reaped success!!! I've managed to solve the problem.

With regards to objects, i can't use the scope="session" but the scope="application" tag. The former will constantly "lock" me out of the application, even if i'm logged on as the administrator.

The code that i've amended is listed below:

<% boolean userAdmin = userid.checkuser();
%>

<% if (session.isNew() == true || userAdmin == false || session.getAttribute("Pass-name") == null)
{
response.sendRedirect(response.encodeRedirectURL("login.jsp"));
}

else {

displayed contents here......


}
%>

0
 
LVL 92

Expert Comment

by:objects
ID: 9861401
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EJB MDB example 4 87
dateFashion java challenge 9 146
Best practices to work on a spring MVC project 1 126
How to Post an If Statement in JSP 3 60
If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question