Struts2实现简单的在线人数统计

  用Strust2框架的知识简单实现一个统计在线人数的问题。

  1 搭建开发环境;(配置文件,jar包等问题)

  2 index.jsp    

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 
11     <!-- <a href="product_input.action">welcome!</a>
12     <br><br> -->
13     <a href="login.action">注册页面</a>
14     <br><br>
15 </body>
16 </html>
View Code

  3 login.jsp  

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="user-login.action" method="post">
11         username:<input type="text" name="username"/>
12         <br><br>
13         <input type="Submit" value="Submit"/>
14     </form>
15 </body>
16 </html>
View Code

     4 success.jsp 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h2>登陆成功!</h2>
11     <br><br>
12     Welcome:${sessionScope.username }
13     <br><br>
14     
15     当前在线人数:${applicationScope.count }
16     <br><br>
17     
18     <a href="user-logout.action">退出登录</a>
19     <br><br>
20     
21     
22     
23 </body>
24 </html>
View Code

  5 logout.jsp  

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h2>退出成功!</h2>
11     --当前人数:${count }
12     <br><br>
13 </body>
14 </html>
View Code

  6 UserAction.java

 1 package com.exe.login;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.dispatcher.SessionMap;
 6 import org.apache.struts2.interceptor.ApplicationAware;
 7 import org.apache.struts2.interceptor.SessionAware;
 8 
 9 public class UserAction implements SessionAware,ApplicationAware{
10 
11     private String username;
12     public void setName(String username) {
13         this.username = username;
14     }
15     
16     //退出
17     public String logout(){
18         Integer count = (Integer)application.get("count");
19         if(count==null)
20             count=0;
21         
22         if(count!= null && count > 0){
23             count--;
24             application.put("count", count);
25         }
26         
27         //使session失效
28         ((SessionMap)session).invalidate();
29         System.out.println("退出一个人, 当前在线人数:"+count);
30         return "logout-success";
31     }
32     
33     public String login(){
34         int index=0;
35         //存储用户信息,放入Session域中
36             //1 获取session:实现SessionAware接口;
37             //2 获取登录信息,通过setter方法;
38             //3 将用户信息存储在session域中;
39         session.put("username", username);
40         
41         //1 获取当前在线人数
42         Integer count = (Integer)application.get("count");
43         if(count==null)
44             count=0;
45         //2 人数+1
46         count++;
47         //3 存储登陆人数
48         application.put("count", count);
49         System.out.println("UserAction 当前在线人数:"+count);
50         
51         return "login-success";
52     }
53 
54     private Map<String,Object> session;
55     
56     @Override
57     public void setSession(Map<String, Object> session) {
58         this.session=session;
59     }
60     
61     private Map<String,Object> application;
62     
63     @Override
64     public void setApplication(Map<String, Object> application) {
65         this.application = application;
66     }
67 }
View Code

  7 Struts.xml  

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5     
 6 <struts>
 7     <!-- 2 -->
 8     <package name="login" extends="struts-default" namespace="/">
 9         <action name="login">
10             <result>/WEB-INF/exe2/login.jsp</result>
11         </action>
12         
13         <action name="user-login" class="com.exe.login.UserAction" method="login">
14             <result name="login-success">/WEB-INF/exe2/success.jsp</result>
15         </action>
16         
17         <action name="user-logout" class="com.exe.login.UserAction" method="logout">
18             <result name="logout-success">/WEB-INF/exe2/logout.jsp</result>
19         </action>
20         
21     </package>
22     
23     <!-- 1 -->
24     <package name="pro" extends="struts-default">
25         <action name="product_input">
26             <result>/WEB-INF/pages/input.jsp</result>
27         </action>
28         
29         <action name="product-save" class="com.exe.pro.Product" method="save">
30             <result name="show">/WEB-INF/pages/show.jsp</result>
31         </action>
32     </package>
33 </struts>
View Code

  

  

原文地址:https://www.cnblogs.com/noaman/p/6138953.html