HttpSessionBindingListener in practice
2004-11-22 16:45
531 查看
HttpSessionBindingListener 是一个接口,继承这个接口的object在我们调用session的setAttribute()方法时将会由容易调用其中该object的方法valueBound()
import javax.servlet.*;
import javax.servlet.http.*;
public class UseridWrapper implements HttpSessionBindingListener {
public String userid = "default";
public UseridWrapper(String id) {
this.userid = id;
}
public void valueBound(HttpSessionBindingEvent e) {
//insert transient user data into the database
}
public void valueUnbound(HttpSessionBindingEvent e) {
//remove transient user data from the database
}
}
//code for doPost() of LoginServlet
public void doPost(HttpServletRequest req, HttpServletResponse res) {
String userid = req.getParameter("userid");
String password = req.getParameter("password");
boolean valid = //validate the userid/password.
if (valid) {
UseridWrapper useridwrapper = new UseridWrapper(userid);
//Sets the UseridWrapper object in the session
req.getSession().setAttribute("useridwrapper", useridwrapper);
} else {
//forward the user to the login page.
}
......
}
import javax.servlet.*;
import javax.servlet.http.*;
public class UseridWrapper implements HttpSessionBindingListener {
public String userid = "default";
public UseridWrapper(String id) {
this.userid = id;
}
public void valueBound(HttpSessionBindingEvent e) {
//insert transient user data into the database
}
public void valueUnbound(HttpSessionBindingEvent e) {
//remove transient user data from the database
}
}
//code for doPost() of LoginServlet
public void doPost(HttpServletRequest req, HttpServletResponse res) {
String userid = req.getParameter("userid");
String password = req.getParameter("password");
boolean valid = //validate the userid/password.
if (valid) {
UseridWrapper useridwrapper = new UseridWrapper(userid);
//Sets the UseridWrapper object in the session
req.getSession().setAttribute("useridwrapper", useridwrapper);
} else {
//forward the user to the login page.
}
......
}
相关文章推荐
- XMLHTTP
- 常用命令
- 可靠数据协议(RDP)--网络大典
- 基于 TCP 的 ISO 传输服务(ITOT)--网络大典
- 打造人脉 成就事业
- 一些杂乱的记录(来至CSDN文档)
- 对辞职冲动的有关思考
- 【热门】2004年十大网络流行语征集ing
- DOS下常用网络相关命令解释
- 东航包头空难以及我的看法
- Why cannot ping a computer in LAN?
- 关键字:oracle_sid,server_name,网络连接,数据库启动
- 11月20号 21号遇到的麻烦事!
- IIS5 HTTP500内部错误解决办法
- 思科交换机Catalyst 3550-24的三层交换与VLAN路由
- 网络游戏外挂设计深度探索
- 短信开发1
- 创作共用解析
- HTTP请求头概述
- 免费空间(zz http://ifstar.net/)