session钝化与活化

来源:互联网 发布:网络大电影投资90万 编辑:程序博客网 时间:2024/04/29 14:41




创建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代码


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


  
    <filter-mapping>
        <filter-name>Login</filter-name>
        <url-pattern>/Login</url-pattern>
    </filter-mapping>
    
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



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+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    当前登录用户:${sesionScope.user.name }
  </body>
</html>


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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body>
    <form action="Login" method="post">
    用户名:<input type="text" name="name"><br>
    密码:<input type="password" name="pwd"><br>
    <input type="submit" value="登录">
    </form>
  </body>
</html>




原创粉丝点击