springmvc-210807-04—拦截器实现简单登录
MyInterceptor01.java(拦截器)
package com.bgy.handler;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor01 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("111111这是MyInterceptor01的preHandler()方法111111");
String loginName = "";
Object attr = request.getSession().getAttribute("name");
if (attr != null) {
loginName = (String) attr;
}
if (!"bgy".equals(loginName)){
request.getRequestDispatcher("/tips.jsp").forward(request,response);
return false;
}
return true;
}
}
MyController.java(中央处理器)
package com.bgy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping("/some.do")
public ModelAndView doSome(String name,Integer age) {
System.out.println("这是MyController的doSome()方法");
ModelAndView mv = new ModelAndView();
mv.addObject("uname",name);
mv.addObject("uage",age);
mv.addObject("msg","拦截器");
mv.setViewName("show");
return mv;
}
}
dispatchServlet.xml(springmvc配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bgy.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.bgy.handler.MyInterceptor01"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" +
request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>拦截器</title>
<base href="<%=basePath%>"/>
</head>
<body>
<form action="some.do" method="post">
<span>拦截器</span>
<br/>
<input type="submit" value="用户登录"/>
</form>
</body>
</html>
login.jsp(登录页面,先运行这个jsp页面,让session有缓存)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录系统</title>
</head>
<body>
<span>登录系统</span>
<%
session.setAttribute("name","bgy");
%>
</body>
</html>
logout.jsp(退出页面,清除session缓存)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>退出系统</title>
</head>
<body>
<span>退出系统</span>
<%
session.removeAttribute("name");
%>
</body>
</html>
show.jsp(登录成功页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>show.jsp</title>
</head>
<body>
WEB-INF/view/show.jsp
<br/>
${msg}
<br/>
姓名:白光一
<br/>
年龄:22
</body>
</html>
tips.jsp(登录失败页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>tips.jsp</title>
</head>
<body>
WEB-INF/view/tips.jsp
<br/>
非bgy用户不得使用
</body>
</html>
我的工程目录
运行结果