文件目录:
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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>MyHttpSessionListener</servlet-name>
<servlet-class>com.g.Login</servlet-class>
<init-param>
<param-name>success</param-name>
<param-value>success.jsp</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpSessionListener</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
Login.java代码:
package com.g; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.omg.CORBA.PUBLIC_MEMBER; public class Login extends HttpServlet{
Map<String, String> users;
//在构造方法中,初始化用户直
public Login(){
users=new HashMap<String, String>();
users.put("zhangsan", "123456");
users.put("lisi", "123456");
users.put("wangwu", "123456");
users.put("zhaoliu", "123456");
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userId=request.getParameter("userId");
String passwd=request.getParameter("passwd");
//匹配用户名与密码.如果一致则记录数加1
if(users.containsKey(userId)&&users.get(userId).equals(passwd)){
request.getSession().setAttribute("user", userId);
request.getSession().setAttribute("count", MyHttpSessionListener.getCount());
}
String success=getInitParameter("success"); //获取初始化参数的success的值
response.sendRedirect(success); //跳转页面
}
}
MyHttpSessionListener.java代码
package com.g; import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener{
private static int count; //统计数
public static int getCount(){
return count;
}
//在session开始时,统计数加1
public void sessionCreated(HttpSessionEvent se) {
MyHttpSessionListener.count++; }
//在Session销毁时,统计数减1
public void sessionDestroyed(HttpSessionEvent se){
MyHttpSessionListener.count--;
}
}
success.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>登陆成功界面</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>
<h3>目前在线人数为:${sessionScope.count}</h3>
<h4>欢迎您:${sessionScope.user }</h4>
</body>
</html>
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>登陆</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" value="" name="userId" /><br />
密码:<input type="password" value="" name="passwd" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>