zoukankan      html  css  js  c++  java
  • java记录在线人数小案例

    文件目录:

    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>
  • 相关阅读:
    day3 集合
    进度条
    day3 文件操作 seek tell 修改
    day3 函数
    同学满分代码,购物车。
    day2杂---三元运算 is
    模块sys os
    day2--列表/元组/字符串/字典
    一、Git配置
    四、TestNG 批量执行脚本Runner.xml
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4430329.html
Copyright © 2011-2022 走看看