servlet中的字符编码过滤器的使用
一:简介
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 stubPrintWriter 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类中可以找到答案
转载于:https://www.cnblogs.com/zongjin/p/7486045.html
servlet中的字符编码过滤器的使用相关推荐
- java 字符过滤器_Java Web---登录验证和字符编码过滤器
什么是过滤器? 在Java Web中,过滤器即Filter.Servlet API中提供了一个Filter接口(javax.servlet.Filter).开发web应用时,假设编写的Java类实现了 ...
- System.Text.Json 中的字符编码
System.Text.Json 中的字符编码 Intro 默认的 System.Text.Json 序列化的时候会把所有的非 ASCII 的字符进行转义,这就会导致很多时候我们的一些非 ASCII ...
- python2发送http不编码_[转]Python 2.x中常见字符编码和解码方面的错误及其解决办法...
Python 2.x中的字符编码,设计的的确不好,导致初学者,甚至是即使用Python很长时间的人,都会经常遇到字符编解码方面的错误. 下面就把一些常见情,尽量的都整理出来,并给出相应的解决办法. 看 ...
- 谈谈Windows程序中的字符编码
sourece:http://www.fmddlmyy.cn/text7.html 谈谈Windows程序中的字符编码 写这篇文章的起因是这么一个问题:我们在使用和安装Windows程序时,有时会看到 ...
- 字符编码过滤器中对特殊的路径进行特殊处理
我做在线打开的时候将word文档转换为html文件,保存在temp文件夹下,然后通过链接打开html文件实现"在线打开word"的功能,结果产生了乱码,原因是在windows系统下 ...
- SSH中设置字符编码防止乱码
1.在web.xml中加入一个过滤器和过滤范围的配置 <filter> <filter-name>encoding</filter-name> <filter ...
- 如何解决python中编码错误的问题_【总结】Python 2.x中常见字符编码和解码方面的错误及其解决办法...
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 对于Python解析器 而Python解析器所干的事情,就是: Python解析器,根据当前的所用的字符串编码类型 此字符串编码类型,是你自己所设置的 不 ...
- linux中修改字符编码
一. ubuntu修改字符编码 1. 添加字符编码,例如zh_CN.UTF-8,有两种方式 方法1:locale-gen zh_CN.UTF-8 #locale-gen命令只在ubuntu中才有 ...
- Qt工作笔记-QXmlStreamReader中的字符编码的坑
以本人这篇博文的源码为例: https://blog.csdn.net/qq78442761/article/details/80501909 源码改成如下所示: #include <QAppl ...
- JAVA中的字符编码
Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的(ASCII码),它为每种语言中的每个字符设 ...
最新文章
- python编程入门经典实例-Python入门经典实例
- 处理数字_9_计算标准差
- UnityShader之Shader格式篇【Shader资料1】
- out类型参数的讲解
- Charles+Android模拟器里抓http请求(配置模拟器教程)
- apache poi_将HTML转换为Apache POI的RichTextString
- Knative Eventing 中如何实现 Registry 事件注册机制
- POJ1338 Ugly Numbers(解法二)【废除!!!】
- Sql Server中Float格式转换字符串varchar方法(转)
- clone,Duplicate复制target XCode iOS
- python revit二次开发_Revit二次开发06
- mysql二级软件_全国计算机等级考试二级MySQL练习软件
- 文本数据挖掘一般步骤
- 上市只是开端,库客音乐用版权打出组合拳
- 论文解读(PairNorm)《PairNorm: Tackling Oversmoothing in GNNs》
- 爆肝三天整理!2021年阿里巴巴社招面试题总结,三轮技术面+HR面,总结的明明白白!
- 为什么所有APP都想访问你的通讯录?
- 数学建模复习——代码记录
- 损失函数--持续更新
- pta 天梯赛 7-3 换硬币 (20 分)day4
热门文章
- 使用 Visual Studio Code 编写 TypeScript
- Beyond Compare如何展开所有子文件夹
- Android TextView 实现文字大小不同和文字颜色不同
- jiangdongsheng
- avrdude: stk500_getsync(): not in sync: resp=0x00
- 利用Nginx 实现负载均衡代理(有图有真相)
- 面试官问:Mybatis Plus 是如何实现动态 SQL 语句的?原理你懂吗?
- 你看,Fastjson 漏洞也太多了吧。。
- YAML 有漏洞被弃用?网友:YAML 不背锅!
- 线程池是如何重复利用空闲线程的?