简单的Filter实现
2017-02-04 14:41
267 查看
1.在xml文件配置过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Filter</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page> <filter> <filter-name>CharsetFilter</filter-name> <filter-class>com.test.filter.CharsetFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.test.filter.LoginFilter</filter-class> <init-param> <param-name>noLoginPath</param-name> <param-value>login.jsp;error.jsp;index.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter> <filter-name>ErrorFilter</filter-name> <filter-class>com.test.filter.ErrorFilter</filter-class> </filter> <filter-mapping> <filter-name>ErrorFilter</filter-name> <url-pattern>/error.jsp</url-pattern> <dispatcher>ERROR</dispatcher> </filter-mapping> <filter-mapping> <filter-name>CharsetFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.建立java类实现Filter接口
1.编码转换案例package com.test.filter; import java.io.IOExcepti 4000 on; 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 CharsetFilter implements Filter{ private FilterConfig config; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String charset = config.getInitParameter("charset"); if(charset == null || charset.equals("")){ charset = "UTF-8"; } request.setCharacterEncoding(charset); response.setCharacterEncoding(charset); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { config = arg0; } }
2.错误显示案例
package com.test.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; public class ErrorFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { System.out.println("error!!!!"); chain.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
3.登录过滤案例
package com.test.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; import javax.servlet.http.HttpSession; public class LoginFilter implements Filter{ private FilterConfig config; @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; HttpSession session = request.getSession(); //获取不用过滤的路径 String noLoginPath = config.getInitParameter("noLoginPath"); //对于不用过滤的路径直接放行 if(noLoginPath != null){ String[] strArray = noLoginPath.split(";"); for(String str:strArray){ if(str == null || str.equals("")) continue; if(request.getRequestURI().indexOf(str) != -1){ chain.doFilter(arg0, arg1); return; } } } //如果session中没有用户信息就跳转到登陆界面,否则放行 if(session.getAttribute("user") == null){ response.sendRedirect("login.jsp"); }else{ chain.doFilter(arg0, arg1); } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub config = arg0; } }
3.建立过滤后显示的页面
1.编码转换案例package com.test.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; public class CharsetFilter implements Filter{ private FilterConfig config; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String charset = config.getInitParameter("charset"); if(charset == null || charset.equals("")){ charset = "UTF-8"; } request.setCharacterEncoding(charset); response.setCharacterEncoding(charset); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { config = arg0; } }
2.错误显示页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my error page. <br> </body> </html>
3.登录过滤页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <span><%=request.getParameter("userName") %></span> <%System.out.println("userName is "+request.getParameter("userName")); %> <span><%=request.getParameter("password") %></span> <%System.out.println("pssword is "+request.getParameter("password")); %> </body> </html>
相关文章推荐
- jQuery plugin items filter
- JSON、XML、TOML、CSON、YAML 大比拼
- XML 与 JSON 优劣对比
- As3.0 xml + Loader应用代码
- 全国哀悼日网站页面变成灰色的filter方法
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- 实例解析Ruby程序中调用REXML来解析XML格式数据的用法
- Ruby中XML格式数据处理库REXML的使用方法指南
- C#实现导出List数据到xml文件的方法【附demo源码下载】
- 用css filter做鼠标滑过图片效果
- C#中如何使用 XmlReader 读取XML文件
- C#针对xml基本操作及保存配置文件应用实例
- Ruby使用REXML库来解析xml格式数据的方法
- Ruby程序中创建和解析XML文件的方法
- Ruby的XML格式数据解析库Nokogiri的使用进阶
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用
- 使用sp_xml_preparedocument处理XML文档的方法