C语言自学完备手册(33篇)
Android多分辨率适配框架
JavaWeb核心技术系列教程
HTML5前端开发实战系列教程
MySQL数据库实操教程(35篇图文版)
推翻自己和过往——自定义View系列教程(10篇)
走出思维困境,踏上精进之路——Android开发进阶精华录
讲给Android程序员看的前端系列教程(40集免费视频教程+源码)
Filter也称之为过滤器,它是JavaWeb三大组件(Servlet、Filter、Listener)之一。它主要用于对用户请求进行预处理和对响应结果进行后处理。也就是说:使用Filter技术可在HttpServletRequest到达Web资源之前拦截客户的HttpServletRequest并根据需要检查HttpServletRequest,亦可修改HttpServletRequest;使用Filter技术可在HttpServletResponse到达客户端之前拦截HttpServletResponse并根据需要检查HttpServletResponse,亦可修改HttpServletResponse;图示如下:
Servlet API中提供了javax.servlet.Filter接口用于实现过滤器Filter。在此,介绍Filter接口中常用的方法及其作用。
public void init(FilterConfig filterConfig) throws ServletException
该方法用来初始化过滤器。
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
该方法用于处理过滤。其中,参数request和response为Web服务器或Filter链中的上一个Filter传递过来的请求和响应对象;参数chain代表当前Filter链的对象。
public void destroy( )
该方法用于释放被Filter对象打开的资源;例如:关闭数据库和IO流
小 结
init( )方法和destroy( )方法仅会被调用一次;而doFilter( )方法可能会被多次调用。
在此,我们通过示例的方式入门Filter
index.html代码如下:
<html>
<head>
<meta charset="utf-8">
<title>indextitle>
head>
<body>
<h2 align="center" style="color: red;">本文作者:谷哥的小弟h2>
<h2 align="center" style="color: red;">博客地址:http://blog.csdn.net/lfdfhlh2>
body>
html>
package cn.com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 638101330297540752L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("Servlet doGet( )");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("Servlet doPost( )");
}
}
package cn.com.filter;
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;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*
* Filter入门示例
*/
public class FilterImpl implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter init( )");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
System.out.println("Filter doFilter( ) before");
//chain.doFilter(request, response);
chain.doFilter(request, response);
System.out.println("Filter doFilter( ) after");
}
@Override
public void destroy() {
System.out.println("Filter destroy( )");
}
}
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Filter01display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>myServletservlet-name>
<servlet-class>cn.com.servlet.MyServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>myServletservlet-name>
<url-pattern>/myServleturl-pattern>
servlet-mapping>
<filter>
<filter-name>filterImplfilter-name>
<filter-class>cn.com.filter.FilterImplfilter-class>
filter>
<filter-mapping>
<filter-name>filterImplfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
请注意:类似于与Servlet,我们需要在配置文件web.xml中配置过滤器filter。
访问地址:http://localhost:8080/Filter01/index.html
访问地址:http://localhost:8080/Filter01/myServlet
对于客户端请求而言:请求先到达拦截器再到达服务端Web资源
对于服务器响应而言:响应先到达拦截器再到达客户端的浏览器
当在Web应用中开发Filter之后需要在web.xml中对其进行注册和配置;其中,在配置时使用< filter-mapping >标签。在此,对于Filter的注册,不再过多强调;我们来一起看看两种常见的Filter的映射。
在< filter-mapping >标签中可使用子标签< url-pattern >设置拦截路径;常用的为/*表示拦截所有请求。
在< filter-mapping >标签中可使用子标签< dispatcher >用于设置依据访问资源的方式进行拦截。< dispatcher >子标签常用取值如下:
1、REQUEST
目标资源被直接访问或者系统重定向后指向该目标资源时Filter才拦截。这也是默认的值
2、FORWARD
目标资源通过RequestDispatcher.forward(request, response)被调用时Filter才拦截
3、ERROR
目标资源是在系统处理异常时被调用
4、INCLUDE
目标资源通过RequestDispatcher.include(request, response)被调用时Filter才拦截
在Web应用程序中可以注册多个Filter程序,每个Filter程序都可针对某具体URL进行拦截。如果多个Filter程序对同一个URL进行拦截,那么这些Filter就会串联成为Filter链(FilterChain滤器链)。Filter链用FilterChain对象来表示,其中的doFilter( )方法表示让Filter链上的当前过滤器放行,使得请求进入下一 Filter,图示如下:
Web服务器根据各Filter在web.xml文件中的注册顺序,依次调用各Filter。在此,以示例形式介绍Filter链的使用。
index.html代码如下:
<html>
<head>
<meta charset="utf-8">
<title>indextitle>
head>
<body>
<h2 align="center" style="color: red;">本文作者:谷哥的小弟h2>
<h2 align="center" style="color: red;">博客地址:http://blog.csdn.net/lfdfhlh2>
body>
html>
package cn.com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 638101330297540752L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("Servlet doGet( )");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("Servlet doPost( )");
};
}
package cn.com.filter;
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;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class FilterImpl1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
System.out.println("Filter1 doFilter( ) before");
//放行
chain.doFilter(request, response);
System.out.println("Filter1 doFilter( ) after");
}
@Override
public void destroy() {
}
}
package cn.com.filter;
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;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class FilterImpl2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
System.out.println("Filter2 doFilter( ) before");
//放行
chain.doFilter(request, response);
System.out.println("Filter2 doFilter( ) after");
}
@Override
public void destroy() {
}
}
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Filter02display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>myServletservlet-name>
<servlet-class>cn.com.servlet.MyServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>myServletservlet-name>
<url-pattern>/myServleturl-pattern>
servlet-mapping>
<filter>
<filter-name>filterImpl1filter-name>
<filter-class>cn.com.filter.FilterImpl1filter-class>
filter>
<filter-mapping>
<filter-name>filterImpl1filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>filterImpl2filter-name>
<filter-class>cn.com.filter.FilterImpl2filter-class>
filter>
<filter-mapping>
<filter-name>filterImpl2filter-name>
<url-pattern>/myServleturl-pattern>
filter-mapping>
web-app>
请求路径:http://localhost:8080/Filter02/myServlet
javax.servlet.FilterConfig接口封装了web.xml配置文件中filter标签下init-param子标签的配置信息。在此,介绍FilterConfig接口中的常用方法及其作用。
public String getFilterName( )
该方法用于获取Filter名字。
public ServletContext getServletContext( )
该方法用于获取ServletContext。
public String getInitParameter(String name)
该方法用于依据初始化参数名称获取其对应的值。
public Enumeration< String > getInitParameterNames( )
该方法用于获取所有初始化参数的名称。
在此,以示例形式介绍FilterConfig的使用。
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Filter06display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>myServletservlet-name>
<servlet-class>cn.com.servlet.MyServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>myServletservlet-name>
<url-pattern>/myServleturl-pattern>
servlet-mapping>
<filter>
<filter-name>filterImplfilter-name>
<filter-class>cn.com.filter.FilterImplfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>filterImplfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
package cn.com.filter;
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;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*
* FilterConfig使用示例
*/
public class FilterImpl implements Filter {
private FilterConfig mFilterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter init( )");
mFilterConfig=filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
System.out.println("Filter doFilter( ) before");
String initParameter = mFilterConfig.getInitParameter("encoding");
System.out.println("initParameter="+initParameter);
chain.doFilter(request, response);
System.out.println("Filter doFilter( ) after");
}
@Override
public void destroy() {
System.out.println("Filter destroy( )");
}
}
在此,以示例形式介绍Filter常见应用场景及其技术特点。
<html>
<head>
<meta charset="UTF-8">
<title>indextitle>
head>
<body>
<h2 align="left" style="color: red;">本文作者:谷哥的小弟h2>
<h2 align="left" style="color: red;">博客地址:http://blog.csdn.net/lfdfhlh2>
<h1>This is index pageh1>
<h1>您已登录 , 欢迎来访h1>
body>
html>
<html>
<head>
<meta charset="UTF-8">
<title>logintitle>
head>
<body>
<form action="/Filter03/LoginServlet" method="post">
账 户:<input type="text" name="username"/><br/><br/>
密 码:<input type="password" name="password"/><br/><br/>
<input type="submit" value="提交">
form>
body>
html>
package cn.com.filter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*
* 利用Filter实现粗粒度的权限控制
*
* 过滤欲访问index.html的请求
* 1 若已经登陆则放行
* 2 若没有登录则跳转至login.html
*/
public class PrivilegeFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
HttpServletRequest httpServletRequest=(HttpServletRequest) request;
HttpSession session = httpServletRequest.getSession();
String username=(String) session.getAttribute("username");
if(username!=null) {
//如果用户名不为空则放行
chain.doFilter(request, response);
} else {
//如果用户名为空则跳转至登录页面
httpServletRequest.getRequestDispatcher("/login.html").forward(request, response);
}
}
@Override
public void destroy() {
}
}
package cn.com.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = -1498309426781988290L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
if(username!=null&&username.length()>0) {
HttpSession session=request.getSession();
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(60 * 30);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
//保存数据至Session中
request.getSession().setAttribute("username", username);
//转发至index.html
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.html");
requestDispatcher.forward(request, response);
}else {
//转发至login.html
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.html");
requestDispatcher.forward(request, response);
}
}
}
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Filter03display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>LoginServletservlet-name>
<servlet-class>cn.com.servlet.LoginServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoginServletservlet-name>
<url-pattern>/LoginServleturl-pattern>
servlet-mapping>
<filter>
<filter-name>PrivilegeFilterfilter-name>
<filter-class>cn.com.filter.PrivilegeFilterfilter-class>
filter>
<filter-mapping>
<filter-name>PrivilegeFilterfilter-name>
<url-pattern>/index.htmlurl-pattern>
filter-mapping>
web-app>
利用EncodingFilter统一处理GET和POST的请求及其响应。
<html>
<head>
<meta charset="UTF-8">
<title>Indextitle>
head>
<body>
<h3 align="left" style="color: red;">本文作者:谷哥的小弟h3>
<h3 align="left" style="color: red;">博客地址:http://blog.csdn.net/lfdfhlh3>
<h5>以下为GET方式提交数据至服务器h5>
<a href="/Filter04/LoginServlet?username=张三&password=123456">提交a>
<br /><br />
<h5>以下为POST方式提交数据至服务器h5>
<form action="/Filter04/LoginServlet" method="post">
用户:<input type="text" name="username" /><br /><br />
密码:<input type="password" name="password" /><br /><br />
<input type="submit" value="提交" />
form>
body>
html>
package cn.com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 8831676289832788100L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String username = request.getParameter("username");
response.getWriter().println(username);
}
}
package cn.com.filter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*
*/
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
//将ServletRequest强转为HttpServletRequest
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//将ServletResponse强转为HttpServletResponse
HttpServletResponse httpServletResponse=(HttpServletResponse) response;
//获取请求方式
String method = httpServletRequest.getMethod();
System.out.println("method="+method);
//设置响应内容的编码方式
httpServletResponse.setContentType("text/html;charset=utf-8");
//设置POST请求时HttpServletRequest的编码方式
httpServletRequest.setCharacterEncoding("utf-8");
//放行
chain.doFilter(httpServletRequest, httpServletResponse);
}
@Override
public void destroy() {
}
}
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Filter04display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>LoginServletservlet-name>
<servlet-class>cn.com.servlet.LoginServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoginServletservlet-name>
<url-pattern>/LoginServleturl-pattern>
servlet-mapping>
<filter>
<filter-name>EncodingFilterfilter-name>
<filter-class>cn.com.filter.EncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>EncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>