JSP利用过滤器解决request中文乱码问题
网络编程 2021-07-05 11:23www.168986.cn编程入门
这篇文章主要介绍了JSP利用过滤器解决request中文乱码问题,感兴趣的小伙伴们可以参考一下
本文为大家分享了JSP用过滤器解决request中文乱码问题,具体内容如下
(1)客户端的数据一般是通过HTTP GET/POST方式提交给服务器,在服务器端用request.getParameter()
读取参数时,很容易出现中文乱码现象。
(2)用过滤器解决request中文乱码问题。
(3)代码如下:
package my; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ChineseFilter implements Filter { //定义了一个过滤器 实现Filter接口 private FilterConfig config = null; public void init(FilterConfig config) throws ServletException { this.config = config; } public void destroy() { config = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("GB2312"); chain.doFilter(request, response); //把过滤后的request对象转发给下一个过滤器处理 } }
(4)部署过滤器。编辑WEB-INF\web.xml文件,添加以下内容:
<filter> <filter-name>cf</filter-name> <filter-class>my.ChineseFilter</filter-class> </filter> <filter-mapping> <filter-name>cf</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping>
这里的<dispatcher></dispatcher>主要是配合RequestDispatcher使用。
- 1.取值为REQUEST时 表示有请求直接来自客户端时,过滤器才能被激活,如果请求是来自RequestDispatcher.forward时不激活;
- 2.取值为FORWARD时 表示如果请求是来自RequestDispatcher.forward时此过滤器才激活;
- 3.取值为INCLUDE时 表示如果请求是来自RequestDispatcher.include时此过滤器才激活;
- 4.取值为ERROR时 表示如果请求是来自RequestDispatcher使用“错误信息页”时此过滤器才激活;
- 5.默认为REQUEST。
(5)创建一个jsp页面检验
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <% String s=request.getParameter("data"); out.print(s); %> </body> </html>
以上就是关于JSP解决request中文乱码问题的方法,希望对大家的学习有所帮助。
上一篇:JSP实现计算器功能(网页版)
下一篇:JSP验证码动态生成方法
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程