Selvlet高级(Java web)

本文详细介绍了如何在Java Web中创建和配置Servlet Filter,从创建Filter类到在web.xml中设置过滤器拦截资源。讨论了<filter>、<filter-mapping>等元素的作用,以及<dispatcher>元素的四种类型:REQUEST、INCLUDE、FORWARD和ERROR。通过实例展示了Filter链的执行过程,并探讨了如何利用FilterConfig接口获取过滤器参数。最后,讲解了如何实现用户登录拦截,包括AutoLoginFilter的使用。
摘要由CSDN通过智能技术生成

 实现第一个Filter
首先在Eclipse中创建一个名为chapter08的Web项目,然后在该项目的src目录下创建一个名为pidan.filter包,最后在该包下创建一个名为MyServlet的Servlet 类,该类用于访问时在浏览器中输出“Hello MyServlet”。
 

package chapter09;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("hello dashuju2004-------<br />");
    }
 
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
 
}

在web.xml对servlet进行如下配置

<servlet>
        <description></description>
        <display-name>MyServlet</display-name>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>pidan.filter.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

运行结果

 创建一个名为MyFilter的Fiter类,该类用于拦截MyServlet程序

package chapter09;
 
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 MyFilter implements Filter {
 
    public MyFilter() {
        // TODO Auto-generated constructor stub
    }
 
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        System.out.println("拦截了相应");
        response.getWriter().print("这是filter输出到页面的内容");
        
        if(true) {
            chain.doFilter(request, response);
        }
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        
    }
 
}

过滤器程序与Servlet程序类似,同样需要在 web.xml文件中进行配置,从而设置它所能拦截的资源;

<filter>
        <filter-name>MyFilter</filter-name>
        <filter-class>pidan.filter.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFilter</filter-name>
        <url-pattern>/MyServlet</url-pattern>
</filter-mapping>

 在上述代码中,过滤器的配置信息中包含多个元素,这些元素分别具有不同的作用,具体如下。
(1)<filter>根元素用于注册一个Filter。
(2)<filter-name>子元素用于设置Filter名称。
(3)<filter-class>子元素用于设置Filter类的完整名称。
(4)<filter-mapping>根元素用于设置一个过滤器所拦截的资源。
(5)<filter-name>子元素必须与<filter>中的<filter-name>子元素相同。
(6)<url-pattern>子元素用于匹配用户请求的URL,例如“/MyServlet”,这个URL还可以使用通配符“*”来表示,例如“*.do”适用于所有以“.do”结尾的Servlet路径。
运行结果:

 

Filter映射
拦截不同方式的访问请求
        在web.xml文件中,一个<filter-mapping>元素用于配置一个Fiter所负责拦截的资源。<filter-mapping>元素中有一个特殊的子元素<dispatcher>,该元素用于指定过滤器所拦截的资源被Servlet容器调用的方式,<dispatcher>元素的值共有4个:
        1) REQUEST
        当用户直接访问页面时,Web容器将会调用过滤器。如果目标资源是通过RequestDispatcher的include()或 forward()方法访问的,那么该过滤器将不会被调用。
        2) INCLUDE
        如果目标资源是通过RequestDispatcher的 include()方法访问的,那么该过滤器将被调用。
除此之外,该过滤器不会被调用。
        3)FORWARD
        如果目标资源是通过RequestDispatcher的forward()方法访问的,那么该过滤器将被调用。
除此之外,该过滤器不会被调用。
        4) ERROR
        如果目标资源是通过声明式异常处理机制调用的,那么该过滤器将被调用。除此之外,过滤器不会被调用。


创建一个名为ForwardServlet的Servlet类,该类用于将请求转发给first.jsp页面:
 

package chapter09;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class ForwardServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/first.jsp").forward(request,response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
 
}

xml中配置信息

<servlet>
		<servlet-name>ForwardServlet</servlet-name>
		<servlet-class>pidan.filter.ForwardServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ForwardServlet</servlet-name>
		<url-pattern>/ForwardServlet</url-pattern>
</servlet-mapping>

新建first.jsp,用于输出内容

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    first.jsp
</body>
</html>

在web.xml文件中,配置过滤器的映射信息,拦截first.jsp页面

<filter>
        <filter-name>ForwardFilter</filter-name>
        <filter-class>pidan.filter.ForwardFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ForwardFilter</filter-name>
        <url-pattern>/first.jsp</url-pattern>
</filter-mapping>

运行结果

 

ForwardFilter没有拦截到ForwardServlet转发的first.jsp页面。

  为了拦截ForwardServlet通过forward()方法转发的first.jsp页面,需要在web.xml文件中的对应过滤器配置信息中增加一个<dispatcher>子元素,将该元素的值设置为FORWARD:
 

<filter>
        <filter-name>ForwardFilter</filter-name>
        <filter-class>pidan.filter.ForwardFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ForwardFilter</filter-name>
        <url-pattern>/first.jsp</url-pattern>
        <dispatcher>FORWARD</dispatcher>
</filter-mapping>

运行结果

 

浏览器窗口显示的是ForwardFilter类中的内容,而first.jsp页面的输出内容没有显示。由此可见,ForwardServlet中通过 forward()方法转发的 first.jsp页面被成功拦截了。 

 Filter链:

MyFilter01.java

package chapter09;
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
/**
 * Servlet Filter implementation class MyFilter01
 */
public class MyFilter01 implements Filter {
 
    /**
     * Default constructor. 
     */
    public MyFilter01() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PrintWriter out=response.getWriter();
        out.write("Hello MyFilter01<br />");
        chain.doFilter(request, response);
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }
 
}

MyFilter02.java

package chapter09;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
/**
 * Servlet Filter implementation class MyFilter02
 */
public class MyFilter02 implements Filter {
 
    /**
     * Default constructor. 
     */
    public MyFilter02() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PrintWriter out=response.getWriter();
        out.write("MyFilter02 Before<br />");
        chain.doFilter(request, response);
        out.write("MyFilter02 After<br />");
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }
 
}

为了防止其他过滤器影响此次Filter链的演示效果,请先在web.xml文件中注释掉其他过滤器的配置信息。然后,将MyFilter01和MyFilter02过滤器的映射信息配置在MyServlet配置信息前面

<filter>
        <filter-name>MyFilter01</filter-name>
        <filter-class>pidan.filter.MyFilter01</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFilter01</filter-name>
        <url-pattern>/MyServlet</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>MyFilter02</filter-name>
        <filter-class>pidan.filter.MyFilter02</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFilter02</filter-name>
        <url-pattern>/MyServlet</url-pattern>
    </filter-mapping>

运行结果

 

MyServlet首先被MyFilter01拦截了,打印出MyFilter01中的内容,然后被MyFilter02拦截,直到MyServlet被MyFilterO2放行后,浏览器才显示出MyServlet中的输出内容。 

 FilterConfig接口:

创建过滤器MyFilter03,来获取 web.xml中设置的参数:
 

package chapter09;
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 MyFilter03 implements Filter {
    private String characterEncoding;
    FilterConfig fc;
 
    public MyFilter03() {
        // TODO Auto-generated constructor stub
    }
 
 
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        characterEncoding=fc.getInitParameter("encoding");
        System.out.println("encoding 初始化的值"+characterEncoding);
        chain.doFilter(request, response);
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        this.fc=fConfig;
    }
 
}

在 web.xml文件中配置过滤器信息。由于Filter链中各个Filter的拦截顺序与它们在web.xml文件中<fiter-mapping>元素的映射顺序一致,因此,为了防止其他Filter影响MyFilter03的拦截效果,这里将MyFilter03映射信息配置在web.xml文件最前端;
 

<filter>
        <filter-name>MyFilter03</filter-name>
        <filter-class>pidan.filter.MyFilter03</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>MyFilter03</filter-name>
        <url-pattern>/MyServlet</url-pattern>
</filter-mapping>

运行结果

 

用户登录: 

创建User类,用来封装用户信息:

package chapter10.entity;
 
public class User {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

创建login.jsp页面,创建一个用户登录的表单:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<html>
<head></head>
<center><h3>用户登录</h3></center>
<body style="text-align: center;">
<form action="${pageContext.request.contextPath }/LoginServlet" 
method="post">
<table border="1" width="600px" cellpadding="0" cellspacing="0" 
align="center" >
    <tr>
        <td height="30" align="center">用户名:</td>
        <td>&nbsp;&nbsp;
        <input type="text" name="username" />${errorMsg }</td>
    </tr>
    <tr>
        <td height="30" align="center">密   &nbsp; 码:</td>
        <td>&nbsp;&nbsp;
          <input type="password" name="password" /></td>
    </tr>
    <tr>
        <td height="35" align="center">自动登录时间</td>
        <td><input type="radio" name="autologin" 
                  value="${60*60*24*31 }" />一个月
            <input type="radio" name="autologin" 
                  value="${60*60*24*31*3 }" />三个月
            <input type="radio" name="autologin" 
                  value="${60*60*24*31*6 }" />半年
            <input type="radio" name="autologin" 
                  value="${60*60*24*31*12 }" />一年
        </td>
    </tr>
    <tr>
        <td height="30" colspan="2" align="center">
                  <input type="submit" value="登录" />
              &nbsp;&nbsp;&nbsp;&nbsp;
            <input type="reset" value="重置" />
        </td>
    </tr>
</table>
</form>
</body>
<html>

编写index.jsp页面,该谢冕用于显示用户的登录信息,如果没有用户登录,在index.jsp页面显示一个超链接;如果已登录,将会显示用户名,以及一个注销的超链接:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>显示登录的用户信息</title>
</head>
<body>
    <br />
    <center>
        <h3>欢迎光临</h3>
    </center>
    <br />
        <c:choose>
            <c:when test="${sessionScope.user==null }">
                    <a href="http://localhost:9999/chap08/login.jsp">用户登录</a>
            
            </c:when>
            <c:otherwise>
                欢迎您:${sessionScope.user.username}    <a href="http://localhost:9999/chap08/LogoutServlet">退出</a>
            </c:otherwise>
        </c:choose>
    
    <hr />
</body>
</html>

编写LoginServlet类,用于处理用户的登录请求;

package chapter10.entity;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
 
 
/**
 * Servlet implementation class LoginServlet
 */
 
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //获得用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //检查用户名和密码
        if ("itcast".equals(username) && "123456".equals(password)) {
            //登录成功
            //将用户状态user对象存入session域
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            request.getSession().setAttribute("user", user);
            //发送自动登录的cookie
            String autoLogin = request.getParameter("autoLogin");
            if (autoLogin != null) {
                //注意cookie中的加密
                Cookie cookie = new Cookie(autoLogin, username + "-"+password);
                cookie.setMaxAge(Integer.parseInt(autoLogin));
                cookie.setPath(request.getContextPath());
                response.addCookie(cookie);
            }
            //跳转至首页
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }else {
            request.setAttribute("errerMsg", "用户名或密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
 
}

用于拦截用户登录的访问请求,判断请求中是否包含用户自动登录;

package chapter10.entity;
 
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 javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
 
/**
 * Servlet Filter implementation class AutoLoginFilter
 */
 
public class AutoLoginFilter implements Filter {
 
    /**
     * Default constructor. 
     */
    public AutoLoginFilter() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest request = (HttpServletRequest) req;
        // place your code here
        
        // pass the request along the filter chain
        //获得一个名为autologin的cookie
        Cookie[] cookies = request.getCookies();
        String autologin = null;
        for (int i = 0; cookies != null && i<cookies.length;i++) {
            if ("autologin".equals(cookies[i].getValue())) {
                autologin = cookies[i].getValue();
                break;
            }
        }
        if (autologin != null) {
            //做自动登录
            String[] parts = autologin.split("-");
            String username = parts[0];
            String password = parts[1];
            //检查用户名和密码
            if ("itcast".equals(username)&&("123456").equals(password)) {
                //登录成功,将用户状态user对象存入session域
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                request.getSession().setAttribute("user", user);
            }
        }
        //放行
        chain.doFilter(request, response);
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }
 
}

创建一个AutoLoginFilter的servlet文件,用于拦截用户访问资源的所有请求

package chapter10.entity;
 
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 javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
 
/**
 * Servlet Filter implementation class AutoLoginFilter
 */
 
public class AutoLoginFilter implements Filter {
 
    /**
     * Default constructor. 
     */
    public AutoLoginFilter() {
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest request = (HttpServletRequest) req;
        // place your code here
        
        // pass the request along the filter chain
        //获得一个名为autologin的cookie
        Cookie[] cookies = request.getCookies();
        String autologin = null;
        for (int i = 0; cookies != null && i<cookies.length;i++) {
            if ("autologin".equals(cookies[i].getValue())) {
                autologin = cookies[i].getValue();
                break;
            }
        }
        if (autologin != null) {
            //做自动登录
            String[] parts = autologin.split("-");
            String username = parts[0];
            String password = parts[1];
            //检查用户名和密码
            if ("itcast".equals(username)&&("123456").equals(password)) {
                //登录成功,将用户状态user对象存入session域
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                request.getSession().setAttribute("user", user);
            }
        }
        //放行
        chain.doFilter(request, response);
    }
 
    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }
 
}

运行结果

 登录

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值