Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regarding logout page in jsp and servlets

802555Aug 18 2008 — edited Aug 27 2008
Hi Friends,
I have created a web page for employee details. When employee enters his details that will be stored in database. Now the problem arises with logout page. When user presses logout, the page is redirected to login page. When he presses back button again its getting logging in for the employee who has just logged in. Can anyone guide me how to resolve this problem. When he presses on back button the page should display with warning message that session has expired. Click here to login. I am posting the code what I have done. And if anyone try to solve using servlets and jsp also no issues. I would be grateful if anyone helps me in advance.

IDE: Netbeans
Database: MS-Access.

Code is as follows

This is html page for employee details:(EmployeeDetails.jsp)

<html>

<head>
<script language=javascript>
function Value()
{
FirstName=document.form1.FirstName.value;
MiddleName=document.form1.MiddleName.value;
LastName=document.form1.LastName.value;
EmployeeID=document.form1.ID.value;

day=document.form1.select1.value;
month=document.form1.select2.value;
year=document.form1.select3.value;
EmailID=document.form1.EmailID.value;

if(FirstName=="")
{
alert("Enter The FirstName");
document.form1.FirstName.focus();
document.form1.FirstName.select();
return true;
}
else if(LastName=="")
{
alert("Enter The LastName");
document.form1.LastName.focus();
document.form1.LastName.select();
return false;
}
else if(EmployeeID=="")
{
alert("Enter EmployeeID");
document.form1.ID.focus();
document.form1.ID.select();
return false;
}
else if(isNaN(EmployeeID))
{
alert("Enter The EmployeeID in Numbers");
document.form1.ID.focus();
document.form1.ID.select();
return false;
}


else if(day=="Day")
{
alert("Select The Day");
document.form1.select1.focus();

return false;
}
else if(month=="Month")
{
alert("Select The Month");
document.form1.select2.focus();

return false;
}
else if(year=="Year")
{
alert("Select The Year");
document.form1.select3.focus();

return false;
}
else if(EmailID=="")
{
alert("Enter EmailID");
document.form1.EmailID.focus();
document.form1.EmailID.select();
return false;
}

}
</script>
</head>

<body BGCOLOR="#ffffcc">

<form name=form1 action="val.jsp" method="post" >

<font color="#FF0000">*</font> <i><b>First Name :&nbsp; </b></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="FirstName" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
logout
<br>

* <i><b>Middle Name:</b></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="MiddleName" size="20"><br>

<font color="#FF0000">* </font><i><b>Last Name :&nbsp;&nbsp;&nbsp;</b></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="LastName" size="20"><br>
<font color="#FF0000">*</font> <i><b>EmployeeID:&nbsp; </b></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="ID" size="20" maxlength="5"><br>






&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(DD)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; / (MM) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ (Yr)<br>

<font color="#FF0000">*</font> <i><b>D.O.J:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></i>
<select name="select1"> <option value="Day" selected>Day</option>
<script language=javascript>
for(i=01;i<=31;i++)
{
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select> /
<select name="select2">
<option value=Month selected>Month</option>
<script language=javascript>
for(i=01;i<=12;i++)
{
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select> /
<select name="select3">
<option value=Year selected>Year</option>
<script language=javascript>
for(i=1975;i<=2009;i++)
{
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select>
<br>
<font color="#FF0000">*</font> <i><b>EmailID:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="EmailID" ><br>
&nbsp;<font color="#FF0000">*&nbsp; </font><i><b>Star with red mark
indicates Mandatory </b></i>
<i> <b>Star with black mark indicates Optional</b></i><br>
<input type="submit" value="Submit" name="Submit" onclick="Value()">
</form>
</body>

</html>


This is for validation purpose(i.e inserting data into database)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" import ="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
String firstname,lastname,empid,day,month,year,emailid,doj;


try

{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbc:odbc:login");
System.out.println("Database Connected");
// System.out.println("name : " +request.getParameter("name"));
firstname=request.getParameter("FirstName");
lastname=request.getParameter("LastName");
empid=request.getParameter("ID");

day=request.getParameter("select1");
month=request.getParameter("select2");
year=request.getParameter("select3");

doj= year + month + day;
emailid=request.getParameter("EmailID");

stmt=con.createStatement();
String query = "insert into EmployeeTable values ('"+firstname+"','"+lastname+"',"+empid+",'"+doj+"','"+emailid+"')";

int i =stmt.executeUpdate(query);




if(i==1)
{
out.println("Data inserted Successfully");
}
else
{
out.println("Data insertion failed ");
}


}
catch(ClassNotFoundException ce)
{
out.print(ce);
}
catch(Exception se)
{
out.print(se);
}

%>
</body>
</html>


This is code for logout page (logout.jsp)



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
session.invalidate();
session=request.getSession(true);

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires",0);
response.sendRedirect("Login.jsp");
%>




</body>
</html>


Another way for logout page what I have tried



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
request.getSession();
session.invalidate();

%>
<jsp:forward page="Login.jsp"/>

</body>
</html>


With Regards,
V.S.Ravi Kiran Balusu.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2008
Added on Aug 18 2008
11 comments
435 views