无为清净楼资源网 Design By www.qnjia.com
本文为大家分享了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://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/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中文乱码问题的方法,希望对大家的学习有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 群星.2022-一不小心喵上你原声大碟【TME影音】【FLAC分轨】
- 《战锤40K星际战士2》卡在正在加入服务器解决方法
- 《勇者斗恶龙怪物仙境3》玩法内容介绍
- 《幸福工厂》无限报错解决方法
- 交错战线原始交易所推荐角色一览
- 战锤40K星际战士2战术职业介绍|战术职业技能效果一览
- 战锤40K星际战士2突击职业介绍|突击职业技能效果一览
- [妙音金曲]群星《悲情咖啡屋》(黑胶)2CD[DTS-WAV]
- 阿兰·达瓦卓玛《A-Lan阿兰唯美歌姬》2CD[DTS-WAV]
- 【小提琴】陈立新《思乡曲》2004[FLAC+CUE]
- 《战地》新作明年初大规模测试!EA已内部测试一年
- 《GTAOL》PC版时隔多年更新反作弊!小助手宣布跑路
- EA称AI是其业务核心!能提高开发效率、节约成本
- 卫华.1990-太阳升【BMG】【WAV+CUE】
- 呼吸乐队.1992-THEBREATHING【深飞】【WAV+CUE】