实现目的:
Struts2实现:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2-LoginAndLogout</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package com.lewa.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 LoginAction implements SessionAware,ApplicationAware{
private static final String SUCCESS = "success";
private static final String LOGOUT="logout";
//与表单域中的属性相相应
private String username;
//JavaBean-style
public void setUsername(String username){
this.username=username;
}
public String execute(){
//将用户信息保存到Session中
//1.获取Session。通过SessionAware接口实现
//2.获取登录信息
//3.把登录信息保存到Session中
session.put("username", username);
//在线人数 +1
//1.获取当前在线人数,从Application中获取
Integer count=(Integer) application.get("count");
if(count==null){
count=0;
}
//使当前在线人数 +1
count++;
application.put("count", count);
return SUCCESS;
}
public String Logout(){
//在线人数 -1
//1.获取在线人数
Integer count=(Integer) application.get("count");
//2.在在线人数>0的情况下 -1
if(count!=null && count!=0){
count--;
application.put("count", count);
}
//使Session失效
((SessionMap)session).invalidate();
return LOGOUT;
}
private Map<String,Object> session;
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
private Map<String,Object> application;
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
}
4.在struts.xml中配置Action
<?
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> <!--以常量配置的形式改动default.properties里所配置的action.extension --> <constant name="struts.action.extension" value="action,do,,"/> <package name="LoginAndLogout" extends="struts-default"> <!-- > Action类默觉得ActionSupport > 运行的方法默觉得execute > result默觉得success 以上三者能够省略不写 --> <!-- 进入登录页面的Action配置 --> <action name="login-ui" class="com.opensymphony.xwork2.ActionSupport" method="execute"> <result name="success">/login.jsp</result> </action> <!-- 登录提交表单的Action配置 --> <action name="login" class="com.lewa.action.LoginAction"> <result>/login-success.jsp</result> </action> <!-- 登出的Action配置 --> <action name="logout" class="com.lewa.action.LoginAction" method="Logout"> <result name="logout">/logout-success.jsp</result> </action> </package> </struts>
5.相应的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>Index</title>
</head>
<body>
<a href="login-ui.do">Login</a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Login</title>
</head>
<body>
<!-- struts2 UI 表单便签 -->
<s:form action="login.do" method="post">
<s:textfield name="username" label="用户名"/>
<s:submit value="Login"/>
</s:form>
</body>
</html>
login-success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>login-success</title>
</head>
<body>
<h3>使用EL</h3>
欢迎,${sessionScope.username }
<br><br>
当前在线人数:${applicationScope.count }
<br><br>
<h3>使用OGNL和Struts2 标签</h3>
欢迎,<s:property value="#session.username"/>
<br><br>
当前在线人数:<s:property value="#application.count"/>
<br><br>
<a href="logout.action">Logout</a>
</body>
</html>
logout-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>Log out</title>
</head>
<body>
当前在线人数:${applicationScope.count }
</body>
</html>