案例
demo2,login.jsp请求提交到hello.jsp,该页面中有两个text,分别使用两个Filter链拦截,验证账号密码是否正确,把账号密码设置到Filter初始化参数中
login.jsp
<body>
<h3>登录</h3>
<form action="hello.jsp" method="post">
username:<input type="text" name="username" /><font color="red">${requestScope.message }</font>
<br>
password:<input type="text" name="password" /><font color="red">${requestScope.message }</font>
<br>
<input type="submit" value="Submit" />
</form>
</body>
hello.jsp
<body>
hello:${param.username }
</body>
PasswordFilter.java
package com.test.demo2UserFilter;
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 {
//获取到FilterConfig对象,因为要获取web应用初始化参数,必须要ServletContext(),
//而ServletContext则需要FilterConfig对象
private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//获取到初始化参数password的参数值
String initpassword = filterConfig.getServletContext().getInitParameter("password");
//获取到表单提交的参数值
String password = request.getParameter("password");
//判断账号是否正确
if(!initpassword.equals(password)){
request.setAttribute("message", "密码不正确!");
request.getRequestDispatcher("/demo2/login.jsp").forward(request, response);
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
UsernameFilter.java
package com.test.demo2UserFilter;
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 UsernameFilter implements Filter {
//获取初始化参数,使用init方法,为filterConfig赋值,因为init()方法可以获取到FilterConfig对象,
//而FilterConfig对象可以获取到初始化参数。
private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//获取到初始化参数username的参数值
String initUser = filterConfig.getInitParameter("username");
//获取到表单提交的参数值
String user = request.getParameter("username");
//判断账号是否正确
if(!initUser.equals(user)){
request.setAttribute("message", "用户名不正确!");
request.getRequestDispatcher("/demo2/login.jsp").forward(request, response);
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
web.xml
<context-param>
<param-name>password</param-name>
<param-value>1230</param-value>
</context-param>
<filter>
<filter-name>usernameFilter</filter-name>
<filter-class>com.test.demo2UserFilter.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>/demo2/hello.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>passwordFilter</filter-name>
<filter-class>com.test.demo2UserFilter.PasswordFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>passwordFilter</filter-name>
<url-pattern>/demo2/hello.jsp</url-pattern>
</filter-mapping>
案例
demo3,禁用浏览器缓存过滤器
cache_a.html
<body>
<a href="cache_b.html">To B Page</a>
</body>
cache_b.html
<body>
<a href="cache_a.html">To A Page</a>
<img alt="" src="02.jpg">
</body>
cacheFilter.java
package com.test.demo3cacheFilter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.demo1Filter.HttpFilter;
/**
* 使用过滤器实现禁用浏览器缓存
*/
public class CacheFilter extends HttpFilter {
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse resp,
FilterChain chain) throws IOException, ServletException {
System.out.println("cache...");
resp.setDateHeader("Expires", -1);
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("Pragme", "no-cache");
chain.doFilter(req, resp);
}
}
web.xml
<filter>
<filter-name>cacheFilter</filter-name>
<filter-class>com.test.demo3cacheFilter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cacheFilter</filter-name>
<url-pattern>/demo3/cache_a.html</url-pattern>
</filter-mapping>
案例
demo4,编码方式过滤器
encoding1.jsp
<body>
<form action="encoding2.jsp" method="post">
name:<input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
encoding2.jsp
<body>
<!-- 解决乱码方案一:使用request的setCharacterEncoding()方法,但是这样需要在每一个页面中都要写这段代码,比较麻烦-->
<%-- <%
request.setCharacterEncoding("UTF-8");
%> --%>
<!-- 解决乱码方案二,所以过滤器就是一个不错的选择 -->
Hello:${param.name }
</body>
Enconding.java
package com.test.demo4encodingFilter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.demo1Filter.HttpFilter;
/**
* 编码方式过滤器,通过配置参数encoding指明使用何种字符编码方式,以处理Html form请求参数的中文问题
*/
public class EncodingFilter extends HttpFilter {
private static final long serialVersionUID = 1L;
private String encoding;
@Override
protected void init() {
this.encoding = getFilterConfig().getServletContext().getInitParameter("encoding");
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse resp,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding(encoding);
System.out.println("编码:"+encoding);
chain.doFilter(req, resp);
}
}
web.xml
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.test.demo4encodingFilter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/demo4/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>