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!

JSP/Servlet session Invalidate.

843840Jan 2 2009 — edited Jan 6 2009
JSP/Servlet session Invalidate.

Hello Everyone,

I am not able to invalidate my session. Kindly help me out.

I am using:

Product Version: NetBeans IDE 6.0.1 (Build 200801291616).
Java: 1.5.0_15; Java HotSpot(TM) Client VM 1.5.0_15-b04.
Server: Apache Tomcat 6.0.14.

The following is my code:

Index.jsp

<%@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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="setses" method="POST">
<input type="text" name="un" value="" />
<br>
<input type="password" name="ps" value="" />
<br>
<input type="submit" value="LOGIN" />
</form>
</body>
</html>
**--**
Setses.java (Servlet)

package ses;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class setses extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String un = request.getParameter("un");
String ps = request.getParameter("ps");
HttpSession myses = request.getSession(true);
try {
myses.setAttribute("un",un);
myses.setAttribute("ps", ps);


out.println("<html>");
out.println("<head>");
out.println("<title>Servlet setses</title>");
out.println("</head>");
out.println("<body>");
out.println("<a href='view.jsp'> view </a>");
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}

**--**
View.jsp

<%@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">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% HttpSession myses = request.getSession();
if(myses.getAttribute("un").equals("")||myses.getAttribute("ps").equals(""))
{
out.print("Invalid ses");
}
else
{
out.print(myses.getAttribute("un"));
out.print(myses.getAttribute("ps")); %>
logout
<%}%>
</body>
</html>


**--**
logout.jsp

<%@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">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
<%
HttpSession myses = request.getSession();
myses.invalidate();
%>
index

</body>
</html>
**--**
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>setses</servlet-name>
<servlet-class>ses.setses</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>setses</servlet-name>
<url-pattern>/setses</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Now the issue is even after logout.jsp if I visit the view.jsp as http://localhost:8080/ses/view.jsp - the session is not invalidated... Kindly tell me where I am making mistake.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 3 2009
Added on Jan 2 2009
20 comments
991 views