您现在的位置:中国下载站学院中心网络编程JSP教程Jsp基础教程 → 文章列表

JBuilder2005实战JSP之日志和部署

作者:佚名  来源:不详  发布时间:2007-4-14 11:55:20   

减小字体 增大字体

 
 
  用户登录和退出日志

  当用户登录系统时,在日志表中插入一条记录,记录用户登录的时间,在用户退出系统时记录用户退出系统的时间。

  我们利用HttpSessionBindingListener接口来完成记录登录和退出日志的功能,该接口中定义了两个方法:

  ·valueBound(HttpSessionBindingEvent event)

  ·valueUnbound(HttpSessionBindingEvent event)

  如果一个类实现了HttpSessionBindingListener接口,当对象通过session.setAttribute()被绑定到Session中时,则对象的接口方法valueBound()被自动调用,当对象从session中移出时(通过调用session.invalidate()、session.removeAttribute()或session自动过期时),valueUnbound()方法将被自动调用。

  下面我们使User.java类实现HttpSessionBindingListener接口,调整后的代码如下所示:

  代码清单 18 实现了HttpSessionBindingListener的User.java

  1. package bookstore;
2. import javax.servlet.http.HttpSessionBindingListener;
3. import javax.servlet.http.HttpSessionBindingEvent;
4. import java.sql.*;
5. import java.text.SimpleDate表单at;
6. import java.util.Date;
7.
8. public class User implements HttpSessionBindingListener
9. {
10.  …
11.  private String loginDatetime;//用户登录时间
12.  …
13.  public void valueBound(HttpSessionBindingEvent event)
14.  {
15.   Connection conn = null;
16.   String sqlStr = "insert into T_LOGIN_LOG(ID, USER_ID, DT_LOGIN) " +
17.       " values(SEQ_LOGIN_LOG_ID.NEXTVAL,?,? )";
18.   try
19.   {
20.    conn = DBConnection.getConnection();
21.    PreparedStatement pStat = conn.prepareStatement(sqlStr);
22.    loginDatetime = getCurrDatetimeStr(); //当前时间串
23.    pStat.setString(1, userId);
24.    pStat.setString(2, loginDatetime);
25.    pStat.executeUpdate();
26.
27.   } catch (SQLException e)
28.   {
29.    throw new RuntimeException(
30.     "用户登陆日志写入出错");
31.   } finally
32.  {
33.  try
34.  {
35.   if (conn != null)
36.   {
37.    conn.close();
38.   }
39.  } catch (SQLException ex)
40.  {
41.   ex.printStackTrace();
42.  }
43.  }
44. }
45.
46. public void valueUnbound(HttpSessionBindingEvent event)
47. {
48.  Connection conn = null;
49.  String sqlStr = " update T_LOGIN_LOG set DT_LONOUT = ? " +
50.      " where USER_ID=? and DT_LOGIN = ?";
51.  try
52.  {
53.   conn = DBConnection.getConnection();
54.   PreparedStatement pStat = conn.prepareStatement(sqlStr);
55.   pStat.setString(1, getCurrDatetimeStr());
56.   pStat.setString(2, userId);
57.   pStat.setString(3, loginDatetime);
58.   pStat.executeUpdate();
59.
60.  } catch (SQLException e)
61.  {
62.   throw new RuntimeException(
63.    "用户退出日志写入出错");
64.  } finally
65.  {
66.   try
67.   {
68.    if (conn != null)
69.    {
70.     conn.close();
71.    }
72.   } catch (SQLException ex)
73.   {
74.    ex.printStackTrace();
75.   }
76.  }
77. }
78.
79. //获取当前时间字串,以yyyyMMddHHmmss格式返回,如20050505010101
80. private static String getCurrDatetimeStr()
81. {
82.  SimpleDate表单at sdf = new SimpleDate表单at("yyyyMMddHHmmss");
83.  return sdf.表单at(new Date());
84. }
85. }


在百度中搜索更多JBuilder2005实战JSP之日志和部署相关网页 转贴于:中国下载站

  • 上一篇文章:JBuilder2005实战JSP之特殊页面
  • 下一篇文章:利用JSP 2.0开发Web应用程序
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!