In this project we:
1)invalidate session using: session.invalidate(); (MySessionInval.java)
2)invalidate session using: session.setMaxInactiveInterval(0); (MySessionTo.java)
Also in web.xml we added
<session-config>
<session-timeout>1 </session-timeout>
</session-config>
This "1" is in minutes. This says if the client doesn't makes any request for 1 minute, then kill the session.
To run this project we need following files:
1)web.xml
2)index.jsp
3)MySessionInval.java
4)MySessionTo.java
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Session Inv</servlet-name>
<servlet-class>com.jexamples.web.MySessionInval</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session Inv</servlet-name>
<url-pattern>/SessionInval.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Session Ex2</servlet-name>
<servlet-class>com.jexamples.web.MySessionTo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session Ex2</servlet-name>
<url-pattern>/SessionSetmax.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1 </session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2)index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body bgcolor="skyblue">
<h3> <a href="SessionInval.do"> click here to invalidate session using: session.invalidate(); </a></h3>
<h3> <a href="SessionSetmax.do"> click here to invalidate session using: session.setMaxInactiveInterval(0); </a></h3>
</body>
</html>
3)MySessionInval.java
package com.jexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MySessionInval extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
long cTime;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body bgcolor='blue'><h1> Session Invalidate Example1<h1> ");
HttpSession session = request.getSession();
cTime = session.getCreationTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
Date resultdate = new Date(cTime);
out.println("<h1>you logged at : "+ (sdf.format(resultdate))+"</h1>");
session.setAttribute("foo", "42");
session.setAttribute("bar", "420");
session.invalidate();
// String foo = (String) session.getAttribute("foo");
//Note: now u cant use session.getAttribute("foo"); bcoz session is already invalidated
//Error: java.lang.IllegalStateException: getAttribute: Session already invalidated
out.println("</body>");
}
}
4)MySessionTo.java
package com.jexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MySessionTo extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body bgcolor='blue'><h1>Session Invalidate Example2<h1> ");
HttpSession session = request.getSession();
session.setAttribute("foo", "42");
session.setMaxInactiveInterval(5); //0 is in seconds, while in web.xml its in minutes
//try using 1/10 and then see o/p
//String foo = (String) session.getAttribute("foo");
//Note: now u cant use session.getAttribute("foo"); bcoz session is already invalidated
//Error: java.lang.IllegalStateException: getAttribute: Session already invalidated
/*
if(session.isNew())
{
out.println("This is a new session");
}
else
{
out.println("Welcome back");
}
*/
//Note: now u cant use session.isNew() bcoz session is already invalidated
//java.lang.IllegalStateException: isNew: Session already invalidated
out.println("</body>");
}
}
1)invalidate session using: session.invalidate(); (MySessionInval.java)
2)invalidate session using: session.setMaxInactiveInterval(0); (MySessionTo.java)
Also in web.xml we added
<session-config>
<session-timeout>1 </session-timeout>
</session-config>
This "1" is in minutes. This says if the client doesn't makes any request for 1 minute, then kill the session.
To run this project we need following files:
1)web.xml
2)index.jsp
3)MySessionInval.java
4)MySessionTo.java
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Session Inv</servlet-name>
<servlet-class>com.jexamples.web.MySessionInval</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session Inv</servlet-name>
<url-pattern>/SessionInval.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Session Ex2</servlet-name>
<servlet-class>com.jexamples.web.MySessionTo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session Ex2</servlet-name>
<url-pattern>/SessionSetmax.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1 </session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body bgcolor="skyblue">
<h3> <a href="SessionInval.do"> click here to invalidate session using: session.invalidate(); </a></h3>
<h3> <a href="SessionSetmax.do"> click here to invalidate session using: session.setMaxInactiveInterval(0); </a></h3>
</body>
</html>
package com.jexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MySessionInval extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
long cTime;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body bgcolor='blue'><h1> Session Invalidate Example1<h1> ");
HttpSession session = request.getSession();
cTime = session.getCreationTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
Date resultdate = new Date(cTime);
out.println("<h1>you logged at : "+ (sdf.format(resultdate))+"</h1>");
session.setAttribute("foo", "42");
session.setAttribute("bar", "420");
session.invalidate();
// String foo = (String) session.getAttribute("foo");
//Note: now u cant use session.getAttribute("foo"); bcoz session is already invalidated
//Error: java.lang.IllegalStateException: getAttribute: Session already invalidated
out.println("</body>");
}
}
package com.jexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MySessionTo extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body bgcolor='blue'><h1>Session Invalidate Example2<h1> ");
HttpSession session = request.getSession();
session.setAttribute("foo", "42");
session.setMaxInactiveInterval(5); //0 is in seconds, while in web.xml its in minutes
//try using 1/10 and then see o/p
//String foo = (String) session.getAttribute("foo");
//Note: now u cant use session.getAttribute("foo"); bcoz session is already invalidated
//Error: java.lang.IllegalStateException: getAttribute: Session already invalidated
/*
if(session.isNew())
{
out.println("This is a new session");
}
else
{
out.println("Welcome back");
}
*/
//Note: now u cant use session.isNew() bcoz session is already invalidated
//java.lang.IllegalStateException: isNew: Session already invalidated
out.println("</body>");
}
}
No comments:
Post a Comment