session钝化与活化

session钝化与活化_第1张图片




创建User实体


package com.oracle.entity;


import java.io.Serializable;


import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;


public class User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable{

private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}

public User(String name, String pwd) {
super();
this.name = name;
this.pwd = pwd;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
/**
* Session绑定和解绑的方法
*/
public void sessionDidActivate(HttpSessionEvent arg0) {
// 活化
System.out.println("Sesion活化,"+arg0.getSource());
}
public void sessionWillPassivate(HttpSessionEvent arg0) {
// 钝化
System.out.println("Sesion钝化,"+arg0.getSource());
}

/**
* Session绑定和解绑的方法
*/
public void valueBound(HttpSessionBindingEvent arg0) {
// 绑定
System.out.println("Session绑定,"+arg0.getName());
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
// 解绑
System.out.println("Session解绑,"+arg0.getName());
}

}


Servlet类

package com.oracle.servlet;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.oracle.entity.User;


public class Login extends HttpServlet {



public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


doPost(request,response);
}



public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String name = request.getParameter("name");
String pwd = request.getParameter("pwd");

//把登录用户的对象存到session
request.getSession().setAttribute("user", new User(name,pwd));

response.sendRedirect("index.jsp");
}


}


web.xml代码



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">
 
 
    This is the description of my J2EE component
    This is the display name of my J2EE component
    Login
    com.oracle.servlet.Login
 



  
   
        Login
        /Login
   

    
    
 
    index.jsp
 




jsp代码

index.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




 
   
    
    My JSP 'index.jsp' starting page


   



 
  
 
    当前登录用户:${sesionScope.user.name }
 


<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



login.jsp




 
   
    
    My JSP 'login.jsp' starting page
    


   





 
  
 
   


    用户名:

    密码:

   
   

 



session钝化与活化_第2张图片

你可能感兴趣的:(javaweb,Sesion钝化与活化实)