您的位置:首页 > 编程语言 > Java开发

struts2统计访问页面人数

2017-07-24 10:49 316 查看
maven项目去http://maven.aliyun.com/nexus/#welcome搜索struts2-core,引入jdk支持的版本jar包



java代码

package cn.et.struts2.lesson01;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsTestCase;
import org.junit.Test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.HttpServer;

import junit.framework.TestCase;

/**
* 		struts2的action类类似servlet
* 		继承ActionSupport实现execute方法
*
* 		servlet HttpServlet doGet  doPost
* 		web.xml 路径映射到该servlet
*
*
* 		servlet是单实例的
* 				线程不安全(synchronize 变量定义在局部方法中)节省内存
* 		struts的action是多实例的 每次请求都会创建新的实例
* 			占用内存多  线程安全
* 		hellWorldAction h=new HelloWorldAction()
*
* @author Administrator
*
*/

public class LoginAction extends ActionSupport{
private String userName;
private String passwrod;

public String login() throws IOException  {
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
Map<String, Object> ac=ActionContext.getContext().getApplication();
Object list=ac.get("userList");
if(list==null){
list=new ArrayList<>();
ac.put("userList", list);
}
List myList=(List)list;
if(!myList.contains("userName")){
myList.add(userName);
}

out.println("欢迎:"+userName);
out.println("当前人数:"+myList.size());
String path=request.getContextPath();
out.println("<a href='"+path+"/lesson01/userout.action?userName="+userName+"'>退出</a>");
return null;
}
public  String loginout() throws IOException, ServletException{
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
Map<String, Object> ac=ActionContext.getContext().getApplication();
List list=(List)ac.get("userList");
list.remove(userName);
request.getRequestDispatcher("login.jsp").forward(request, response);
return null;
}
public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPasswrod() {
return passwrod;
}

public void setPasswrod(String passwrod) {
this.passwrod = passwrod;
}

}


struts2.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>
<!-- 如果需要使用struts2高级功能   必须继承struts-default -->
<package name="mypackage" extends="struts-default">
<!--
浏览器 网址 :localhost:8080/sl/hello
mothod=方法名
-->
<action name="user" class="cn.et.struts2.lesson01.LoginAction" method="login"></action>

</package>

</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: