過濾器Filter(攔截jsp頁面的跳轉)案例:


 創建一個 Filter , class類: 其繼承於 接口 Filte(接口導包:import javax.servlet.Filter;)

在 web.xml 文件中配置並映射該 Filter. 其中 url-pattern 指定該 Filter 可以攔截哪些資源, 即可以通過哪些 url 訪問到該 Filter,並進行攔截;

 

案例:username=Tom,password=1234,設計Filter類,及jsp頁面實現,輸入username和password是否等於Tom和1234,不等攔截index.jsp頁面跳轉到hello.jsp頁面,等的或,在在hello,顯示;

 

1.建立UserNameFilter類:繼承於接口Filter(接口導包:import javax.servlet.Filter;)

package com.lanqiao.javatest;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.catalina.connector.Request;

public class UserNameFilter implements Filter{

    //構造方法
    public UserNameFilter(){
        
    }
    public void destroy() {
        
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        String initUser=filterConfig.getInitParameter("username");
        String username=request.getParameter("username");
        
        if(!initUser.equals(username)){
            request.setAttribute("message", "用戶名不正確!!!");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return ;
        }
        chain.doFilter(request, response);
        
    }

    private FilterConfig filterConfig;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
        
    }


}

 

2.建立PasswordFilter類:繼承於接口Filter(接口導包:import javax.servlet.Filter;)

package com.lanqiao.javatest;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class PasswordFilter implements Filter {

    public PasswordFilter() {
        // TODO Auto-generated constructor stub
    }
    @Override
    public void destroy() {
        
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        String initPassword=filterConfig.getServletContext().getInitParameter("password");
        String password=request.getParameter("password");
        if(!initPassword.equals(password)){
            request.setAttribute("message", "密碼不正確!!!");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return ;
        }
        chain.doFilter(request, response);
        
    }

    private FilterConfig filterConfig;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
        
    }

    

}

 

3.在WEB-FIN下的web.xml文件下的配置和映射:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>day-12</display-name>
  
  <filter>
      <filter-name>UserNameFilter</filter-name>
      <filter-class>com.lanqiao.javatest.UserNameFilter</filter-class>
      <init-param>
          <param-name>username</param-name>
          <param-value>Tom</param-value>
      </init-param>
  </filter>
  
  <filter-mapping>
      <filter-name>UserNameFilter</filter-name>
      <url-pattern>/hello.jsp</url-pattern>
  </filter-mapping>
  
  <filter>
      <filter-name>password</filter-name>
      <filter-class>com.lanqiao.javatest.PasswordFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>password</filter-name>
      <url-pattern>/hello.jsp</url-pattern>
  </filter-mapping>
  <context-param>
      <param-name>password</param-name>
      <param-value>1234</param-value>
  </context-param>
  
</web-app>

 

4.index.jsp頁面:輸入username和password;

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <font color="red">${message }</font>
    <br><br>
    <form action="hello.jsp" method="post">
        username:<input type="text" name="username" /><br>
        password:<input type="password" name="password"/><br>
        <input type="submit" value="Submit"/>
    </form>
    
</body>
</html>

 

5.hello.jsp頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    Hello:${param.username }
</body>
</html>

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM