使用拦截器判断用户是否登录

该博客详细介绍了如何利用拦截器来判断用户是否登录,从登录界面到后台的action、service、dao层层深入,涉及User相关的Java类及XML配置文件,并重点讲解了 PrivilegeInterceptor 拦截器的实现和在 struts.xml 中的配置。
摘要由CSDN通过智能技术生成

本文主要讲述使用拦截器实现判断用户是否登录,在下边代码主要从页面向后台,后台从action--》service--》dao过程,最后展示配置的方式进行描述。

登录界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/frameset.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<STYLE type=text/css>
BODY {
	FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: 宋体
}
TD {
	FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: 宋体
}
</STYLE>

<META content="MSHTML 6.00.6000.16809" name=GENERATOR></HEAD>
<BODY>
<FORM id=form1 name=form1 action="${ pageContext.request.contextPath }/user_login.action" method=post target="_parent">

<DIV id=UpdatePanel1>
<DIV id=div1 
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>
<DIV id=div2 
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>


<DIV>&nbsp;&nbsp; </DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=900 align=center border=0>
  <TBODY>
  <TR>
    <TD style="HEIGHT: 105px"><IMG src="images/login_1.gif" 
  border=0></TD></TR>
  <TR>
    <TD background=images/login_2.jpg height=300>
      <TABLE height=300 cellPadding=0 width=900 border=0>
        <TBODY>
        <TR>
          <TD colSpan=2 height=35></TD></TR>
        <TR>
          <TD width=360></TD>
          <TD>
          	<s:actionerror/>
            <TABLE cellSpacing=0 cellPadding=2 border=0>
              <TBODY>
              <TR>
                <TD style="HEIGHT: 28px" width=80>登 录 名:</TD>
                <TD style="HEIGHT: 28px" width=150><INPUT id=txtName 
                  style="WIDTH: 130px" name="user_code"></TD>
                <TD style="HEIGHT: 28px" width=370><SPAN 
                  id=RequiredFieldValidator3 
                  style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">请输入登录名</SPAN></TD></TR>
              <TR>
                <TD style="HEIGHT: 28px">登录密码:</TD>
                <TD style="HEIGHT: 28px"><INPUT id=txtPwd style="WIDTH: 130px" 
                  type=password name="user_password"></TD>
                <TD style="HEIGHT: 28px"><SPAN id=RequiredFieldValidator4 
                  style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">请输入密码</SPAN></TD></TR>
              <TR>
                <TD styl
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己的状态改为 `THINKING`,并打印一些信息,包括哲学家的编号和放下的叉子的编号。接下来,它调用 `test` 函数,以测试是否有其他哲学家可以进餐在 Java 中,可以通过实现拦截器(Interceptor)来判断用户是否过期。具体实现步骤如下: 1. 创建一个拦截器类并实现 HandlerInterceptor 接口。 2. 在 preHandle 方法中获取当前请求的 HttpSession。最后,它释放信号量 `mutex`,表示它已经放下了叉子。 ```c 对象,并从中获取用户信息。 3. 判断用户信息是否存在且是否过期,如果过期则跳转到登录 void put_away_forks(int i) { sem_wait(&mutex); state[i] = THINKING; printf("Philosopher页面,否则放行请求。 以下是一个简单的拦截器示例代码: ``` public class UserInterceptor implements Handler %d puts fork %d and %d down\n", i+1, LEFT+1, i+1); printf("PhilosopherInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); if (user == null || user.isExpired()) { response.sendRedirect %d is thinking\n", i+1); test(LEFT); test(RIGHT); sem_post(&mutex); } ("/login"); return false; } return true; } } ``` 在上面的代码中,User 类表示用户 ``` 11. `philosopher` 函数用于模拟每个哲学家的行为。在这个函数中,哲信息,isExpired() 方法用于判断用户是否过期。在 preHandle 方法中,首先获取 HttpSession 对象,然后学家会不停地重复拿起叉子、进餐和放下叉子的过程。 ```c 从中获取用户信息。如果用户信息不存在或已过期,则重定向到登录页面,并返回 false,拦截器会 void *philosopher(void *arg) { while (1) { int *i = arg; grab_forks(*i); 终止请求的继续执行。如果用户信息存在且未过期,则放行请求,返回 true。最后,需要将拦截器注册到 Spring MVC 中,以便它能够拦截请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值