1.首页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <script type="text/javascript" src="../js/jquery-1.7.2.js"></script> 10 <body> 11 <form action="../SessionServlet"> 12 用户:<input type="text" name="userName"/><br/> 13 <button type="submit">提交</button> 14 <button type="button">退出</button> 15 </form> 16 </body> 17 <script type="text/javascript"> 18 $(":button").click(function(){ 19 20 location.href="../DestroySession"; 21 }) 22 23 24 </script> 25 </html>
2.监听器(HttpServletListener)
1 package com.zdsofe.work; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.PrintWriter; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 import javax.servlet.http.HttpSessionEvent; 10 import javax.servlet.http.HttpSessionListener; 11 12 public class Listener1 implements HttpSessionListener { 13 14 @Override 15 public void sessionCreated(HttpSessionEvent arg0) { 16 17 String mess="创建成功"; 18 writeLog(mess); 19 } 20 21 @Override 22 public void sessionDestroyed(HttpSessionEvent arg0) { 23 24 String mess="销毁OK"; 25 writeLog(mess); 26 } 27 28 29 public void writeLog(String message) 30 { 31 PrintWriter out=null; 32 try { 33 out=new PrintWriter(new FileOutputStream("D://log.txt", true)); 34 try { 35 out.print(new SimpleDateFormat("yyyy-MM-dd").format(Date.class.newInstance())); 36 out.println(message); 37 } catch (InstantiationException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } catch (IllegalAccessException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 } catch (FileNotFoundException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 out.flush(); 49 out.close(); 50 } 51 }
3.创建session的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 /** 13 * Servlet implementation class SessionServlet 14 */ 15 @WebServlet("/SessionServlet") 16 public class SessionServlet extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 /** 20 * @see HttpServlet#HttpServlet() 21 */ 22 public SessionServlet() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 27 /** 28 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 29 */ 30 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 HttpSession session= request.getSession(); 32 session.setAttribute("name", request.getParameter("userName")); 33 34 35 } 36 37 /** 38 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 39 */ 40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 41 // TODO Auto-generated method stub 42 } 43 44 }
4.销毁session的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * Servlet implementation class DestroySession 12 */ 13 @WebServlet("/DestroySession") 14 public class DestroySession extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 /** 18 * @see HttpServlet#HttpServlet() 19 */ 20 public DestroySession() { 21 super(); 22 // TODO Auto-generated constructor stub 23 } 24 25 /** 26 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 27 */ 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 request.getSession().invalidate(); 30 } 31 32 /** 33 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 37 } 38 39 }
5.在web.xml中配置监听器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>webProject5</display-name> 4 <listener> 5 <listener-class>com.zdsofe.work.Listener1</listener-class> 6 </listener> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 <welcome-file>default.html</welcome-file> 12 <welcome-file>default.htm</welcome-file> 13 <welcome-file>default.jsp</welcome-file> 14 </welcome-file-list> 15 </web-app>
6.监听器(ServletContextListener)
1 package com.zdsofe.work; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletContextEvent; 5 import javax.servlet.ServletContextListener; 6 7 public class Listener2 implements ServletContextListener { 8 9 @Override 10 public void contextDestroyed(ServletContextEvent event) { 11 System.out.println("杀驴逼"); 12 13 } 14 15 16 @Override 17 public void contextInitialized(ServletContextEvent event) { 18 19 //加载监听的时候获得项目名称 20 ServletContext context= event.getServletContext(); 21 String name= context.getServletContextName(); 22 context.setAttribute("name", name); 23 //获取初始化参数 24 String s= context.getInitParameter("lister"); 25 System.out.println(s); 26 27 } 28 29 }
7.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>webProject5</display-name> 4 <listener> 5 <listener-class>com.zdsofe.work.Listener2</listener-class> 6 </listener> 7 <context-param> 8 <param-name>lister</param-name> 9 <param-value>utf-8</param-value> 10 </context-param> 11 <welcome-file-list> 12 <welcome-file>index.html</welcome-file> 13 <welcome-file>index.htm</welcome-file> 14 <welcome-file>index.jsp</welcome-file> 15 <welcome-file>default.html</welcome-file> 16 <welcome-file>default.htm</welcome-file> 17 <welcome-file>default.jsp</welcome-file> 18 </welcome-file-list> 19 </web-app>