Struts2+DAO层实现实例03——添加监听器跟踪用户行为
实例说明
根据上两次的成品进行二次加工。
加入Listener,监听用户的登陆注销情况。
所用知识说明
采用SessionBindingListener对Session进行监听。
同时,Action中获取Application,Session,request的方法(在此只列出一种)更多方法
public class LoginAction {
private Map request;
private Map session;
private Map application; public String execute() {
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
request.put("username1", "jingjing1");
session.put("username2", "jingjing2");
application.put("username3", "jingjing3");
return "success";
}
}
代码实例
登陆控制UserManagment
package UserBlock; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import javafx.application.Application;
import org.apache.struts2.views.util.ContextUtil; import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.ArrayList;
import java.util.Map; /**
* Servlet监听器,控制记录用户的登陆注销信息
* Created by Richard on 2017/6/16.
*/
public class UserManagment {
class Userlistener implements HttpSessionBindingListener{
private String username; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
ActionContext context = ActionContext.getContext();
Map application = context.getApplication();
ArrayList online= (ArrayList) application.get("online");
if(online==null){
online=new ArrayList();
}
online.add(username);
application.put("online",online);
} @Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
ActionContext context = ActionContext.getContext();
Map application = context.getApplication();
ArrayList online= (ArrayList) application.get("online");
online.remove(username);
} } /*
登陆
首先判别是否登陆
已经登陆---->return
没有登陆---->获取对应的Session,存入对应用户名的listener
*/ public void login(String username){
if(islogin(username)){
return;
}else{
Userlistener newUser=new Userlistener();
newUser.setUsername(username);
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
session.put("username",newUser);
} } /*
判断是否登陆:
判别条件Session中是否有对应的该用户名的Listener
有--->已经登陆,未注销
无--->没有登陆
*/
public boolean islogin(String username){
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
Userlistener judge= (Userlistener) session.get("username");
if(judge!=null){
return true;
}else {
return false;
}
} /*
注销
首先判断是否登陆
已经登陆--->移除Listener--->true
没有登陆--->false */ public boolean logoff(String username){
if(islogin(username)){
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
session.remove(username);
return true;
}else {
return false;
}
}
/*
人数统计
返回Session中List的Size。
*/ public int returnNum(){
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
ArrayList online= (ArrayList) session.get("online");
if(online==null){
online=new ArrayList();
}
return online.size();
} /*
list返回
*/
public ArrayList returnlist(){
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
ArrayList online= (ArrayList) session.get("online");
if(online==null){
online=new ArrayList();
}
return online;
}
}
主页in.jsp
<%--
Created by IntelliJ IDEA.
User: Richard
Date: 2017/6/16
Time: 21:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>WelCome to My Struts Page</h1>
<hr>
当前登录的用户:${param.username}<br>
<hr>
当前所有登陆的用户:
<table border=1 width=200>
<s:iterator value="#application.online" var="user">
<tr <s:if test="#user.odd"> style="background-color: dimgray" </s:if> >
<td><s:property value="#user.count"></s:property> </td>
<td><s:property value="#user"></s:property></td>
</tr>
</s:iterator> </table> <a href="login.jsp">注销</a>
</body>
</html>
Action:
package UserBlock; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList;
import java.util.Map;
import java.util.logging.LogManager; /**
* Created by Richard on 2017/6/16.
* 继承ActionSupport实现一个Action
* 登陆界面通过loginuser.action调用login()方法来处理数据
* login()方法中通过ActionContext调用Session对象,存入输入错误的数据
* 通过addActionMessage()方法向页面输出相关信息
* 注册页面通过reguser.action调用reg()方法来处理数据
*/
public class UserAction extends ActionSupport {
private String INDEX="index";
private String LOGIN="login";
private String REG="register";
private String username;
private String password;
private String compassword;
private UserDao user;
private UserManagment managment; public String getCompassword() {
return compassword;
} public void setCompassword(String compassword) {
this.compassword = compassword;
} 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;
} public String login(){
try{
managment=new UserManagment();
user=new UserDao();
ArrayList result=user.up_select(username);
if(result.size()>0){
User aim= (User) result.get(0);
if(aim.getPassword().equals(password)){
/*登陆成功*/
managment.login(username);
return INDEX;
}else{
ActionContext applicton=ActionContext.getContext();
Map session=applicton.getSession();
int count;
if(session.get("count")==null){
count=0;
}else{
count= (int) session.get("count");
}
if(count>=3){
addActionMessage("错误次数过多");
count=0;
session.put("count",count);
return LOGIN;
}else{
count++;
addActionMessage("您输入的用户名或密码错误"+count);
session.put("count",count);
return LOGIN;
} }
}else{
addActionMessage("该用户不存在,已经跳转到注册页面");
return REG;
}
}catch (Exception e){
addActionError(e.getMessage());
System.out.println(e.getMessage());
e.printStackTrace();
return LOGIN;
}
} public String reg(){
try{
managment=new UserManagment();
user=new UserDao();
ArrayList result=user.up_select(username);
if(result.size()>0)
{
addActionMessage("该用户已经存在");
return REG;
}
else{
if(user.insert(username,password)){
managment.login(username);
return INDEX;
}else{
addActionMessage("发生未知错误,请重试!");
return REG;
} }
}catch (Exception e){
addActionError(e.getMessage());
return REG;
}
}
}
Git源码
---->更新注销,注销的效果可以在列表中观察。
Struts2+DAO层实现实例03——添加监听器跟踪用户行为的更多相关文章
- Struts2+DAO层实现实例02——搭建DAO基本框架并与Struts2组合
实例内容 创建DAO(Data Access Oject)接口:BaseDAO 创建其实例化类:UserDAO 用于获取数据库struts中的userinfo表中的内容 创建User的Java Bea ...
- Struts2+DAO层实现实例01——搭建Struts2基本框架
实例内容 利用Strust2实现一个登陆+注册功能的登陆系统. 实现基础流程:
- 系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层
系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="j ...
- 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试
什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...
- DAO层注入HibernateTemplate的两种方式
-------------------------siwuxie095 DAO 层注入 HibernateTemplat ...
- [转]JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...
- 01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层
作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 这是一个整合IDEA+Maven+SSM框架的高并发的商品秒杀项目.我们将分为以下几篇文章来进行详 ...
- ssh_maven的搭建之dao层的开发
之前都是使用我们传统的方式进行引入jar包,现在我们使用maven进行管理依赖,这样,我们的jar就不需要我们进行管理,而且,我们的maven还可以进行项目构建,一个项目从编写源代码到编译,测试,运行 ...
- 给dao层注入jdbcTemplate时的一个强行bug(jdbcDaoSupport不要随便用!用了要记得!)
记录Dao层一个鱼唇至极的错误 这一天我在使用Spring的进行注解配置项目时, 我的Idea给我抛了一个如下的错误: Exception in thread "main" org ...
随机推荐
- webapi中配置返回的时间数据格式
web api返回的是标准格式UTC时间,如果要转成我们需要的格式,可以在WebApiConfig.cs的Register函数中新增以下配置来定义返回的时间类型格式: //配置返回的时间类型数据格式 ...
- C#声明方法
一.声明方法 方法是类中用于执行计算或其它行为的成员. 方法可以分为: ?静态方法:可以通过类进行访问.?实例方法:可以通过类的对象进行访问. 1. C#方法的声明 声明方法的语法格式如下: 访问修饰 ...
- python 多进程,多线程,协程
在我们实际编码中,会遇到一些并行的任务,因为单个任务无法最大限度的使用计算机资源.使用并行任务,可以提高代码效率,最大限度的发挥计算机的性能.python实现并行任务可以有多进程,多线程,协程等方式. ...
- 谈谈两种标准库类型---string和vector
两种最重要的标准库---string和vector string和vector是两种最重要的标准库类型,string表示可变长的字符序列,vector存放的是某种给定类型对象的可变长序列. 一.标准库 ...
- Linux入门篇(五)——Shell(一)
这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...
- 【jQuery】input框输入手机号自动填充空格
<input type="tel" id="tel"> $("#tel").keyup(function(){ _self = ...
- Flask初学者:蓝图Blueprint
蓝图这个名字好像就是根据单词Blueprint字面意思来,跟平常我们理解的蓝图完全挂不上钩,这里蓝图就是指Blueprint. 使用蓝图的好处是可以将不同功能作用的视图函数/类视图放到不同的模块中,可 ...
- Git-Git协同与工作协同
Git支持的协议 首先来看看数据交换需要使用的协议. Git提供了丰富的协议支持,包括:SSH.GIT.HTTP.HTTPS.FTP.FTPS.RSYNC及前面已经看到的本地协议等.各种不同协议的UR ...
- 1,flask简介
一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...
- JS:关于JS字面量及其容易忽略的12个小问题
简要 问题1:不能使用typeof判断一个null对象的数据类型 问题2:用双等号判断两个一样的变量,可能返回false 问题3:对于非十进制,如果超出了数值范围,则会报错 问题4:JS浮点数并不精确 ...