呵呵,最近好象问了好多的问题哦,那我就再问问哈,别嫌我麻烦哈,呵呵
我就是想在网页上设个session监听器,当用户登陆时,就把该用户信息表中的一个字段"userStatus"设为"在线"
当session过期时就把userStatus"设为"离线",不知道我这样做可不可以,我的代码如下:
登陆的代码:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<base href="<%=basePath%>">
<title>登陆页面</title>
</head>
<body>
<div style="z-index:0;position:absolute;left:420px;top:200px";>
<form action="UserLog_Servlet" method=post name=form id=form onsubmit="return check()">
<table border=1 height="35" width="320" align="center" style="size:12">
<tr>
<td colspan="2" align=center><font color=red>
用户登陆
</font></td>
</tr>
<tr>
<td align=right>
名称:
</td>
<td align=center>
<input type=text name=loginName size=20>
</td>
</tr>
<tr>
<td align=right>密码:</td>
<td align=center><input type=password name=passWord size=20> </td>
</tr>
<tr>
<td colspan="2" align=center> <input type=submit value="登陆"> <input type=reset value="重置"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Servlet如下:
package myServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import myJavaBean.UserInfo_Bean;
import myListener.onLine_Listener;
public class UserLog_Servlet extends HttpServlet {//主要对登陆做处理
/**
*
*/
private static final long serialVersionUID = 4842855968990446843L;
public UserLog_Servlet(){
super();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
MD5_Encoding md5=new MD5_Encoding();
String LoginName=request.getParameter("loginName");
String PassWord= request.getParameter("passWord");
UserInfo_Bean userBean=new UserInfo_Bean();
UserReg_Form user=userBean.getuser(LoginName,PassWord);//要定义
if (user!=null){
request.getSession(true).setAttribute("loginName", LoginName);
onLine_Listener listener=new onLine_Listener();
listener.setUser(LoginName);
request.getSession(true).setAttribute("list",listener);
request.getSession().setMaxInactiveInterval(10);
try{
request.getRequestDispatcher("index.jsp").forward(request, response);
}catch(Throwable T){
//要写日记
}
}else{
request.setAttribute("errorMsg","名称或是密码错了,请重新输入!");
request.getRequestDispatcher("user/Login.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet( request, response);
}
}
我的UserInfo_Bean 如下:////里面定义了一些数据库操作方法,
package myJavaBean;
import DBHandle.DBConn;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.*;
import myForm.UserReg_Form;
public class UserInfo_Bean {
Connection conn;
Statement stmt;
ResultSet rs;
String sql=null;
public void statusOnline(String loginName){
try{
conn=DBConn.getconn();//连接数据库的,
stmt=conn.createStatement();
sql="update UserInfo_table set userStatus='在线' where loginName='"+loginName+"'";
stmt.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
}
public void statusLeave(String loginName){
try{
conn=DBConn.getconn();
stmt=conn.createStatement();
sql="update UserInfo_table set userStatus='离线' where loginName='"+loginName+"'";
stmt.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
}
}
我的Listener如下:////监听器
package myListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import DBHandle.DBConn;
import myJavaBean.UserInfo_Bean;
public class onLine_Listener implements HttpSessionListener {
private String user;
private HttpServletRequest request;
UserInfo_Bean userbean=new UserInfo_Bean();
public onLine_Listener(){
user="";
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public void sessionCreated(HttpSessionEvent arg0) {
// TODO 自动生成方法存根
user=request.getParameter("loginName");
userbean.statusOnline(user);
System.out.print(user+"上线");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO 自动生成方法存根
userbean.statusLeave(user);
System.out.print(user+"离线");
}
}
不知道我的这个方法能不能行哦,
哪为大哥我改改啊,先谢谢了,