使用监听查看在线用户

(-)使用监听查看在线用户

1 显示层(一)登录界面index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
<CENTER>
<H2>在线系统</H2>
<FORM action="CheckInfo" method="post">
请输入用户名
<INPUT type="text" name="name"><BR>
请输入密码
<INPUT type="password" name="password"><BR>
<INPUT type="submit" value="登录">
</FORM>
</CENTER>
</body>
</html>

2 显示层(二)index.jsp


<%@ page language="java" import="java.util.*,com.*" pageEncoding="UTF-8"%>
<%@ page import="com.UserListener" %>
<%
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>
<%
UserList userList=UserList.getUserList();
UserListener ul=new UserListener();

%>
<body>
<CENTER>
<H2>在线用户名单</H2>
<P>
<TEXTAREA rows="10" cols="20">
<%
Vector vector=userList.getList();
if(vector!=null&&vector.size()>0)
{
for(int i=0;i<vector.size();i++)
{
out.println(vector.elementAt(i));
}
}
%>
</TEXTAREA>
</P>
</CENTER>
</body>
</html>


3 Servlet处理层CheckInfo.java

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class CheckInfo extends HttpServlet {

private ServletRequest session;
/**
* Constructor of the object.
*/
public CheckInfo() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out
// .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
// out.println("<HTML>");
// out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
// out.println(" <BODY>");
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the POST method");
// out.println(" </BODY>");
// out.println("</HTML>");
// out.flush();
// out.close();
UserList userList=UserList.getUserList();
UserListener ul=new UserListener();
String name=request.getParameter("name");
String password=request.getParameter("password");
//测试
// System.out.println(name);
// System.out.println(password);
if(check(name,password))
{
//System.out.println("haha");
ul.setUser(name);
//System.out.println(name);
//session.setAttribute("list",ul);
request.getSession(true).setAttribute("list",ul);
userList.addUser(ul.getUser());
response.sendRedirect("index.jsp");
}
else
{
response.sendRedirect("error.jsp");
}
}
public boolean check(String name,String password)
{
boolean check=false;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
//String sql="select * from student where username"+"=?"+" and "+"password"+"=?";
String sql="select * from student where username=? and password=?";
PreparedStatement stat=con.prepareStatement(sql);
stat.setString(1,name);
stat.setString(2,password);
ResultSet rs=stat.executeQuery();
if(rs.next())
{
System.out.println("OK");
check=!check;
}
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return check;
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}


4 模型层(-)UserList.java

package com;

import java.util.Vector;

public class UserList
{
Vector container;
private static UserList userList=new UserList();
private UserList()
{
container=new Vector();
}
public static UserList getUserList()
{
return userList;
}
public void addUser(String user)
{
if(user!=null)
{
container.addElement(user);
}
}
public Vector getList()
{
return container;
}
public void removeUser(String user)
{
if(user!=null)
{
container.removeElement(user);
}
}
}
5 模型层(二)UserListener.java

package com;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class UserListener implements HttpSessionBindingListener
{
private String user;
private UserList userList=UserList.getUserList();
public UserListener()
{
user="";
}
public void setUser(String user)
{
this.user=user;
}
public String getUser()
{
return user;
}
public void valueBound(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
System.out.println(user+"用户上线");
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
userList.removeUser(user);
System.out.println(user+"用户下线");
}
}


6 显示层(三)error.jsp


<%@ page language="java" import="java.util.*" pageEncoding="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 'error.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>
错误<A href="index.html">fanhui</A>
</body>
</html>


几点说明

1 需要一个数据库,这里的表名为student

2 使用了HttpSessionBingdingListener接口。此接口是唯一一个不用web.xml中设置的接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值