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

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

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

  2 index.jsp    

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <!-- <a href="product_input.action">welcome!</a>
<br><br> -->
<a href="login.action">注册页面</a>
<br><br>
</body>
</html>

  3 login.jsp  

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user-login.action" method="post">
username:<input type="text" name="username"/>
<br><br>
<input type="Submit" value="Submit"/>
</form>
</body>
</html>

4 success.jsp 

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>登陆成功!</h2>
<br><br>
Welcome:${sessionScope.username }
<br><br> 当前在线人数:${applicationScope.count }
<br><br> <a href="user-logout.action">退出登录</a>
<br><br> </body>
</html>

  5 logout.jsp  

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

  6 UserAction.java

 package com.exe.login;

 import java.util.Map;

 import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware; public class UserAction implements SessionAware,ApplicationAware{ private String username;
public void setName(String username) {
this.username = username;
} //退出
public String logout(){
Integer count = (Integer)application.get("count");
if(count==null)
count=0; if(count!= null && count > 0){
count--;
application.put("count", count);
} //使session失效
((SessionMap)session).invalidate();
System.out.println("退出一个人, 当前在线人数:"+count);
return "logout-success";
} public String login(){
int index=0;
//存储用户信息,放入Session域中
//1 获取session:实现SessionAware接口;
//2 获取登录信息,通过setter方法;
//3 将用户信息存储在session域中;
session.put("username", username); //1 获取当前在线人数
Integer count = (Integer)application.get("count");
if(count==null)
count=0;
//2 人数+1
count++;
//3 存储登陆人数
application.put("count", count);
System.out.println("UserAction 当前在线人数:"+count); return "login-success";
} private Map<String,Object> session; @Override
public void setSession(Map<String, Object> session) {
this.session=session;
} private Map<String,Object> application; @Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}

  7 Struts.xml  

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- 2 -->
<package name="login" extends="struts-default" namespace="/">
<action name="login">
<result>/WEB-INF/exe2/login.jsp</result>
</action> <action name="user-login" class="com.exe.login.UserAction" method="login">
<result name="login-success">/WEB-INF/exe2/success.jsp</result>
</action> <action name="user-logout" class="com.exe.login.UserAction" method="logout">
<result name="logout-success">/WEB-INF/exe2/logout.jsp</result>
</action> </package> <!-- 1 -->
<package name="pro" extends="struts-default">
<action name="product_input">
<result>/WEB-INF/pages/input.jsp</result>
</action> <action name="product-save" class="com.exe.pro.Product" method="save">
<result name="show">/WEB-INF/pages/show.jsp</result>
</action>
</package>
</struts>

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

  

上一篇:VC++ 获取exe或者dll版本信息


下一篇:微软 Build 2014开发者大会干货整理-1