Structs2 HelloWorld-03 复习-01

本文介绍了一个基于Struts2框架实现的简单登录和登出功能,包括配置文件structs.xml的设置、登录页面及成功页面的展示方式,并展示了如何在登录时更新在线人数计数以及登出时清除会话。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实现一个登陆,当登陆成功,显示欢迎和当前在线人数

  1. 获取session
  2. 获取登录信息
  3. 把用户信息加入session中
  4. 获取当前的在线人数
  5. 使当前的在线人数加一

二、实现登出,当前登陆人数减一,并使session失效

  1. 获取在线人数数量减一
  2. session失效:强转为sessionMap类型,调用invalidate方法
三、实现代码
structs.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<!-- 配置structs2 可以受理的请求的扩展名 使用constant来设置 -->

	<constant name="structs.action.extension" value="action,do,"></constant>

	<package name="loginUI" namespace="/" extends="struts-default">

		<action name="login-ui">
			<result>/Login.jsp</result>
		</action>
		<action name="userlogin" class="com.weixuan.structs2.UserLogin">
			<result name="login-success">/login-success.jsp</result>
		</action>
		<action name="logout" class="com.weixuan.structs2.UserLogin" method="logout">
			<result name="logout-success">/Login.jsp</result>
		</action>
	</package>
</struts>    
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<form action="userlogin.action" method="post">

		username:<input type="text" name="username" /> password:<input
			type="password" name="password" />
		<input type="submit" value="submit">
	</form>
</body>
</html>

login-success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login-success.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	Wlecome : ${sessionScope.username }
	<br>
	<br>
	<br> 
	Count on line : ${applicationScope.count }

	<br>
	<br>
	<br>
	<a href="logout.action">Logout</a>
</body>
</html>

核心类
package com.weixuan.structs2;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;

public class UserLogin implements SessionAware,ApplicationAware{
	
	private Map<String,Object> session;
	private Map<String,Object> application;
	private String username;
	public String execute(){
		
		//1、获取session
		//2、获取登录信息
		//3、把用户信息加入session中
		session.put("username", username);
		//在线人数加一
		//1、获取当前的在线人数
		//2、使当前的在线人数加一
		//3、
		Integer count  = (Integer)application.get("count");
		if(count==null)
			count=0;
		count++;
		application.put("count", count);
		return "login-success";
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
	
	public String logout(){
		
		//数量减一,获取在线人数
		Integer count  = (Integer)application.get("count");
		if(count !=null && count>0){
			count--;
			application.put("count", count);
		}
		//session失效,强转为sessionMap类型,调用invalidate方法
		SessionMap sm = (SessionMap) session;
		sm.invalidate();
		return "logout-success";
	}
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值