一:简介

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:创建字符编码过滤器类:

 public class FirstFilter implements Filter {
protected String encoding = null;
protected FilterConfig filter = null; /**
* Default constructor.
*/
public FirstFilter() {
// TODO Auto-generated constructor stub
} /**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filter = null; } /**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
if(encoding != null)
{
request.setCharacterEncoding(encoding);
response.setContentType("text/html; charset="+encoding);
} // pass the request along the filter chai
chain.doFilter(request, response);//来将请求发送给下一个过滤器
} /**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {//初始化将web.xml中的初始化参数的信息赋给encoding
// TODO Auto-generated method stub
this.filter = fConfig;
this.encoding = filter.getInitParameter("encoding");
} }

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">
<display-name>MyfirstServlet</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>
<!-- servlet的配置信息-->
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.johnny.test.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<!-- filter的配置信息-->
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>com.johnny.test.FirstFilter</filter-class>
<!-- 初始化参数-->
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/*</url-pattern> <!--关联所有的url该使用该过滤器-->
<dispatcher>REQUEST</dispatcher><!--当客户端直接请求时进行过滤器处理-->
<dispatcher>FORWARD</dispatcher><!-- 当客户端用forward()方法请求时。。。。-->
</filter-mapping> </web-app>

3:login.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>
<!--action中放的是web.xml中映射servlet的url,它会直接将信息发送到servlet中进行处理-->
<form action = "helloworld" method = "post">
<table>
<tr>
<td> 登录名:</td><td><input type = "text" name = "usrname"></td>
</tr>
<tr>
<td>密码:</td><td><input type = "password" name = "password"></td> </tr> </table>
<input type = "submit" name = "submit" value = "登录">
</form>
</body>
</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类中可以找到答案

servlet中的字符编码过滤器的使用的更多相关文章

  1. Spring的字符编码过滤器CharacterEncodingFilter

    Spring中的字符编码过滤器,用来解决我们项目中遇到的编码问题. 使用方式特别友好,在web.xml加入: <filter> <description>字符集过滤器</ ...

  2. 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题

    当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...

  3. Servlet字符编码过滤器

    在Java Web程序开发中,由于Web容器内部使用编码格式并不支持中文字符集,所以,处理浏览器请求中的中文数据就会出现乱码的现象.由于Web容器使用了ISO-8859-1的编码格式,所以在Web应用 ...

  4. Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生

    同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...

  5. Jsp字符编码过滤器

    通过此过滤器,可以实现统一将编码设置为UTF-8. 1.首先在web.xml中配置,添加如下代码: <!-- 过滤器 --> <filter> <filter-name& ...

  6. Java Web---登录验证和字符编码过滤器

    什么是过滤器? 在Java Web中,过滤器即Filter.Servlet API中提供了一个Filter接口(javax.servlet.Filter).开发web应用时,假设编写的Java类实现了 ...

  7. SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题

    1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...

  8. spring设置字符编码过滤器

    一.在web.xml中的配置 <!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>chara ...

  9. 深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器

    引用 前几天在论坛上看到一篇帖子,是关于Struts2.0中文乱码的,楼主采用的是spring的字符编码过滤器(CharacterEncodingFilter)统一编码为GBK,前台提交表单数据到Ac ...

随机推荐

  1. CSS3新增文本属性实现图片点击切换效果

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. POJ-1182 食物链(并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 75814   Accepted: 22528 Description ...

  3. LR的响应时间与使用IE所感受时间不一致的讨论

    在做性能测试时,有时碰到这样一种情况,使用性能工具LR测试出来的响应时间比实际使用IE感受到的时间要长,例如,实际使用IE打开一个系统时只需要1~2秒,而使用LR跑一个用户所得出的结果可能是8秒.10 ...

  4. Selenium对于对话框alert,confirm,prompt的处理

    html 源码: <html>                  <head>                      <title>Alert</titl ...

  5. java代码_按要求输出相应沙漏

    package Callatz;/* 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印******* *****   ***    *   ...

  6. 常见C/C++笔试、面试题(二)

    我自己总结过一些常见的C++面试题,那个是基于一个同学的腾讯面经所问问题,再加上知识点扩展进行了总结,这个是网上之前就有的版本,比较基础,有些题目总结一下,不能忘了基础: 1.求下面函数的返回值( 微 ...

  7. jdbc电话本项目

    整体思路:在登陆之后才能查看自己的电话本,电话本中包含用户名,联系人名字,电话,性别,分类: 1.登陆注册页面--数据库User表,注册登陆使用 2.电话本的前段显示,用表格和表单, 3.创建存取的电 ...

  8. java array to list

    背景 想把数组转为list,使用list的判断元素是否存在的方法,结果发现一个坑,int类型的数组失败了 步骤 public static void main(String[] args) { int ...

  9. git命令中带有特殊符号如@

    使用带用户密码clone的方式:git clone https://username:password@remote 当username和password中含有特殊符号会导致出错,因为为http的请求 ...

  10. oracle之备份详解

    1.冷备份(执行冷备份前必须关闭数据库) 物理备份(备份物理数据库文件) 2.热备份(热备份是当数据库正在运行时进行数据备份的过程.执行热备份的前提是:数据库运行在可归档日志模式.适用于24X7不间断 ...