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>
- session钝化与活化
- session钝化与活化
- JavaWeb---session活化与钝化
- session的钝化与活化
- Session的活化与钝化
- javaEE之--------------session的活化与钝化
- session的钝化与活化技术
- session的钝化和活化
- session的活化和钝化
- 监听器 HttpSessionBindingListener ;HttpSessionActivationListener; session的活化与钝化
- Session的活化与钝化钝化和通过代码(Java)发送邮件(QQ,新浪)
- HttpSessionActivationListener钝化与活化监听器
- Servlet监听器/Session钝化和活化
- IDEA之Session的活化和钝化
- 对于session序列化跟session的钝化与活化的粗浅理解
- session的序列化以及session的钝化和活化
- Session活化和钝化,要实现Serializable接口
- Tomcat 之 Session的活化和钝化 源码分析
- Coursera Machine Learning ex2第三周 week3编程全套满分题目+注释(包括选做optional)
- Unity3D
- USB通信开发资料搜集
- 【C++】两个字符串相加
- 手写朴素贝叶斯文本分类
- session钝化与活化
- AI GOD:1012: Attack
- 算法工程师~~修炼之道
- merge关键字
- iOS MRC 下 block 循环引用问题
- 关于cmake qmake make makeile之间的关系问题
- STM32F0 Slave I2C配置
- js加密
- python3 学习笔记(一)