一:简介
Servlet过滤器是客户端和目标资源的中间层组件,主要是用于拦截客户端的请求和响应信息。如当web容器收到一条客户端发来的请求
web容器判断该请求是否与过滤器相关联,如果相关联就交给过滤器进行处理,处理完可以交给下一个过滤器或者其他业务,当其他业务完成
需要对客户端进行相应的时候,容器又将相应交给过滤器进行处理,过滤器处理完响应就将响应发送给客户端。
注意:上面话中的几个问题
1:web容器是如何判断请求和过滤器相关联。
2:过滤器是如何将处理完的请求交给其他过滤器的
前提知识:过滤器有三个接口
1:Filter接口中有三个方法
1>public void init(FilterConfig filterConfig)//这个参数中含有web.xml文件中的初始化参数,可以用来对请求进行处理
2>public void doFilter(ServletRequest request,ServletRequest response,FilterChain chain)//要注意第三个参数它主要是用来将处理完的请求发送到下一个过滤器
3>public void destroy()//销毁过滤器
2:FilterChain
1>void doFilter(ServletRequest request,ServletRequest response)
3:FilterConfig这个暂时不介绍
二:举例
字符编码过滤器:
1:创建字符编码过滤器类:
1 public class FirstFilter implements Filter { 2 protected String encoding = null; 3 protected FilterConfig filter = null; 4 5 6 7 8 /** 9 * Default constructor. 10 */ 11 public FirstFilter() { 12 // TODO Auto-generated constructor stub 13 } 14 15 /** 16 * @see Filter#destroy() 17 */ 18 public void destroy() { 19 // TODO Auto-generated method stub 20 this.encoding = null; 21 this.filter = null; 22 23 24 } 25 26 /** 27 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 28 */ 29 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 30 // TODO Auto-generated method stub 31 // place your code here 32 if(encoding != null) 33 { 34 request.setCharacterEncoding(encoding); 35 response.setContentType("text/html; charset="+encoding); 36 } 37 38 // pass the request along the filter chai 39 chain.doFilter(request, response);//来将请求发送给下一个过滤器 40 } 41 42 /** 43 * @see Filter#init(FilterConfig) 44 */ 45 public void init(FilterConfig fConfig) throws ServletException {//初始化将web.xml中的初始化参数的信息赋给encoding 46 // TODO Auto-generated method stub 47 this.filter = fConfig; 48 this.encoding = filter.getInitParameter("encoding"); 49 } 50 51 }
2:在web.xml中配置相关信
<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"> 3 <display-name>MyfirstServlet</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list><!-- servlet的配置信息--> 12 <servlet> 13 <servlet-name>HelloWorld</servlet-name> 14 <servlet-class>com.johnny.test.HelloWorld</servlet-class> 15 </servlet> 16 <servlet-mapping> 17 <servlet-name>HelloWorld</servlet-name> 18 <url-pattern>/helloworld</url-pattern> 19 </servlet-mapping><!-- filter的配置信息--> 20 <filter> 21 <filter-name>FirstFilter</filter-name> 22 <filter-class>com.johnny.test.FirstFilter</filter-class> <!-- 初始化参数--> 23 <init-param> 24 <param-name>encoding</param-name> 25 <param-value>GBK</param-value> 26 </init-param> 27 </filter> 28 <filter-mapping> 29 <filter-name>FirstFilter</filter-name> 30 <url-pattern>/*</url-pattern> <!--关联所有的url该使用该过滤器--> 31 <dispatcher>REQUEST</dispatcher><!--当客户端直接请求时进行过滤器处理--> 32 <dispatcher>FORWARD</dispatcher><!-- 当客户端用forward()方法请求时。。。。--> 33 </filter-mapping> 34 35 36 37 </web-app>
3:login.jsp登录界面:
1 <%@ page language="java" contentType="text/html; charset=GB18030" 2 pageEncoding="GB18030"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 7 <title>Insert title here</title> 8 </head> 9 <body><!--action中放的是web.xml中映射servlet的url,它会直接将信息发送到servlet中进行处理--> 10 <form action = "helloworld" method = "post"> 11 <table> 12 <tr> 13 <td> 登录名:</td><td><input type = "text" name = "usrname"></td> 14 </tr> 15 <tr> 16 <td>密码:</td><td><input type = "password" name = "password"></td> 17 18 </tr> 19 20 </table> 21 <input type = "submit" name = "submit" value = "登录"> 22 </form> 23 </body> 24 </html>
4:编写servlet来进行处理login.jsp发来的信息
package com.johnny.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloWorld */ @WebServlet("/HelloWorld") public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloWorld() { 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 } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); String name = request.getParameter("usrname"); if(name != null && !name.equals("")) { out.println("你好"+name); out.println("欢迎来到英雄联盟"); //request.getRequestDispatcher("index.jsp").forward(request, response); } else { out.println("请输入用户名"); } out.print("<br><a href = index.jsp>返回</a>"); out.flush(); out.close(); } }
5:index.jsp页面来处理用户没有输入信息的处理
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> <form action = "helloworld" method = "post"> <p> 请你输入你的中文名字: <input type = "text" name = "usrname"> <input type = "submit" name = "submit"> </p> </form> </body> </html>
6:上面的问题答案在web.xml和filter类中可以找到答案
时间: 2024-10-07 04:18:01