码迷,mamicode.com
首页 > 编程语言 > 详细

java记录在线人数小案例

时间:2015-04-15 23:09:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

文件目录:
技术分享

web.xml:

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <servlet>
13       <servlet-name>MyHttpSessionListener</servlet-name>
14       <servlet-class>com.g.Login</servlet-class>      
15       <init-param>
16           <param-name>success</param-name>
17           <param-value>success.jsp</param-value>
18       </init-param>
19       <load-on-startup>0</load-on-startup>
20   </servlet>
21   <servlet-mapping>
22       <servlet-name>MyHttpSessionListener</servlet-name>
23       <url-pattern>/login</url-pattern>
24   </servlet-mapping>
25 </web-app>

 

 

 

Login.java代码:

 1 package com.g;
 2 
 3 import java.io.IOException;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import org.omg.CORBA.PUBLIC_MEMBER;
13 
14 public class Login extends HttpServlet{
15 Map<String, String> users;
16 //在构造方法中,初始化用户直
17     public Login(){
18         users=new HashMap<String, String>();
19         users.put("zhangsan", "123456");
20         users.put("lisi", "123456");
21         users.put("wangwu", "123456");
22         users.put("zhaoliu", "123456");
23     }
24     
25     @Override
26         protected void doGet(HttpServletRequest req, HttpServletResponse resp)
27                 throws ServletException, IOException {
28         doGet(req, resp);
29         }
30     @Override
31         protected void doPost(HttpServletRequest request, HttpServletResponse response)
32                 throws ServletException, IOException {
33                 request.setCharacterEncoding("UTF-8");
34                 String userId=request.getParameter("userId");
35                 String passwd=request.getParameter("passwd");
36                 //匹配用户名与密码.如果一致则记录数加1
37                 if(users.containsKey(userId)&&users.get(userId).equals(passwd)){
38                     request.getSession().setAttribute("user", userId);
39                     request.getSession().setAttribute("count", MyHttpSessionListener.getCount());
40                 }
41                 String success=getInitParameter("success");    //获取初始化参数的success的值
42                 response.sendRedirect(success);        //跳转页面
43         }
44 }

MyHttpSessionListener.java代码

 1 package com.g;
 2 
 3 import javax.servlet.http.HttpSessionEvent;
 4 import javax.servlet.http.HttpSessionListener;
 5 
 6 public class MyHttpSessionListener implements HttpSessionListener{
 7     private static int count; //统计数
 8     public static int getCount(){
 9         return count;
10     }
11     //在session开始时,统计数加1
12     public void sessionCreated(HttpSessionEvent se) {
13         MyHttpSessionListener.count++;
14         
15     }
16     //在Session销毁时,统计数减1
17     public void sessionDestroyed(HttpSessionEvent se){
18         MyHttpSessionListener.count--;
19     }
20 }

success.jsp代码

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>登陆成功界面</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24     <h3>目前在线人数为:${sessionScope.count}</h3>
25     <h4>欢迎您:${sessionScope.user }</h4>    
26   </body>
27 </html>

index.jsp代码

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>登陆</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24     <form action="login" method="post">
25         姓名:<input type="text" value="" name="userId" /><br />
26         密码:<input type="password" value="" name="passwd" /><br />
27         <input type="submit" value="提交" /> 
28     </form>
29   </body>
30 </html>

 

java记录在线人数小案例

标签:

原文地址:http://www.cnblogs.com/guoyansi19900907/p/4430329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!