创建一个 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>

  

05-07 10:16