随笔- 106  文章- 0  评论- 4 

过滤器拦截类

package com.ssm.filter;

import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(urlPatterns = {"/*"})
public class myFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest) servletRequest;
        System.out.println(httpServletRequest.getRequestURI());
        if(httpServletRequest.getRequestURI().equals("/user/login")){
            filterChain.doFilter(servletRequest,servletResponse);
            System.out.println("合法请求");
        }
        Object user = httpServletRequest.getSession().getAttribute("user");
        if(user==null){
            System.out.println("非法请求");
            httpServletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);
        }else{
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
View Code

web.xml文件配置

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!--初始化参数-->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <!--强制使用UTF-8编码-->
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>

  <!--中央调度器-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>
View Code

login.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆</title>
</head>
<body>
    <div>
        <form action="/user/login" method="post">
            用户名:<input type="text" name="usercode"/>
            密码:<input type="password" name="userpassword"/>
            <input type="submit" value="登陆"/>
        </form>
    </div>
</body>
</html>
View Code

welcome.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: FLC
  Date: 2019/11/14
  Time: 10:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>欢迎</title>
</head>
<body>
    欢迎光临:${user.username}
</body>
</html>
View Code

  在没有登录的情况下访问首页,则直接跳转到登录页面,进行登录验证;

  

 

 

 

 posted on 2019-11-14 17:19  wnwn  阅读(...)  评论(... 编辑 收藏