Struts2之统计用户在线人数案例

 

统计用户在线人数案例

 

1. 添加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>登陆</title>
</head>
<body>
    
    <h4>可用账户:rabbitx/123456</h4>
    <hr/>
    <form action="login.do" method="POST"> 
        <fieldset>
            <legend>登陆</legend>
                                    账户:<input type="text" name="username">
                                    密码:<input type="password" name="password">
                <input type="submit" value="登陆">
        </fieldset>
    </form>
    
</body>
</html>

 

<%@ 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>在线用户数统计</title>
</head>
<body>
    
    <h2>当前用户:${sessionScope.username}</h2>
    <h2>在线用户数:${applicationScope.count}</h2>
    <a href="logout.do">注销</a>
</body>
</html>

 

2. 添加Action

 

package org.rabbitx.web.struts2.user.action;

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 Map<String, Object> session;
	
	private Map<String, Object> application;
	
	private String username;
	
	private String password;
	
	@Override
	public void setSession(Map<String, Object> session) {
        this.session = session;		
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		 this.application = application;				
	}
	
	public String login()
	{
		if("rabbitx".equals(username) && "123456".equals(password))
		{
			session.put("username", username);
			Integer count = (Integer)application.get("count");
            if(null == count)
            {
            	count = 0;
            }
            application.put("count", ++count);
			return "success";
		}else
		{
			
			return "fail";
		}
	}
	
	
	@SuppressWarnings("rawtypes")
	public String logout()
	{
		Integer count = (Integer)application.get("count");
		if(count != null && count > 0)
		{
			application.put("count", --count);
		}
		
		((SessionMap)session).invalidate();
		System.out.println("session invalidate ...");
		
		return "success";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

 

3. 配置Action

 

	<package name="online-user-statistic" extends="struts-default">
	
	    <action name="login" class="org.rabbitx.web.struts2.user.action.UserAction" method="login">
            <result name="success">/statistics/homepage.jsp</result>
            <result name="fail">/statistics/login.jsp</result>
        </action>
        
        <action name="logout" class="org.rabbitx.web.struts2.user.action.UserAction" method="logout">
            <result name="success">/statistics/login.jsp</result>
            <result name="fail">/statistics/login.jsp</result>
        </action>
        
	</package>

 

 

你可能感兴趣的:(struts2)