Struts2实现显示在线人数

墨蓝 2022-04-23 10:30 236阅读 0赞

登录成功以后将用户信息放入Session域中(在线人数+1)

在线人数信息保存在application域中

登出时使session直接失效(在线人数-1)

index

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h3>欢迎登陆</h3>
  11. <form action="user_login" method="post">
  12. 用户名 <input type="text"name="username"/><br/>
  13. 密码 <input type="password"name="password"/><br/>
  14. <input type="submit" value="登录"/><br/>
  15. </form>
  16. </body>
  17. </html>

success

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>登录成功</title>
  8. </head>
  9. <body>
  10. <h1>欢迎${sessionScope.loginUser}登录!</h1>
  11. <h3>当前在线人数:${applicationScope.count}</h3>
  12. <h1><a href="user_logout">登出</a></h1>
  13. </body>
  14. </html>

logout

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>欢迎下次再来</h1>
  11. </body>
  12. </html>

Struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  7. <constant name="struts.devMode" value="true" />
  8. <package name="default" namespace="/" extends="struts-default">
  9. <action name="user_login" class="com.Action.UserAction"
  10. method="login">
  11. <result>/WEB-INF/views/success.jsp</result>
  12. <allowed-methods>*</allowed-methods>
  13. </action>
  14. <action name="user_logout" class="com.Action.UserAction"
  15. method="logout">
  16. <result>/WEB-INF/views/baby.jsp</result>
  17. <allowed-methods>*</allowed-methods>
  18. </action>
  19. </package>
  20. </struts>

UserAction

  1. package com.Action;
  2. import java.util.Map;
  3. import org.apache.struts2.dispatcher.SessionMap;
  4. import org.apache.struts2.interceptor.ApplicationAware;
  5. import org.apache.struts2.interceptor.SessionAware;
  6. public class UserAction implements SessionAware,ApplicationAware{
  7. private String username;
  8. private String password;
  9. public String getUsername() {
  10. return username;
  11. }
  12. public void setUsername(String username) {
  13. this.username = username;
  14. }
  15. public String getPassword() {
  16. return password;
  17. }
  18. public void setPassword(String password) {
  19. this.password = password;
  20. }
  21. public String logout() {
  22. ((SessionMap)session).invalidate();
  23. return "success";
  24. }
  25. public String login() {
  26. if (session.get("loginUser") != null) {
  27. return "success";
  28. }
  29. System.out.println(username+password);
  30. session.put("loginUser",username);
  31. Integer count = (Integer) application.get("count");
  32. if (count == null) {
  33. count = 0;
  34. }
  35. application.put("count", count+1);
  36. return "success";
  37. }
  38. private Map<String, Object>session;
  39. @Override
  40. public void setSession(Map<String, Object> arg0) {
  41. this.session = arg0;
  42. }
  43. private Map<String, Object>application;
  44. @Override
  45. public void setApplication(Map<String, Object> arg0) {
  46. this.application = arg0;
  47. }
  48. }

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读

    相关 监听器:统计在线人数

            接着,我来写一个监听器的案例来巩固学习监听器的知识,便于日后的查阅和复习。大概分为以下几个步骤: 1.编写统计人数的Servlet,实现特定的监听器接口 2