乱码详情
 总结:讨论了使用GET和POST方法,控制台和JSP页面显示的问题。
最终发现:在servlet或者过滤器中添加:request.setCharacterEncoding("utf-8");

        res.setContentType("text/html;charset=utf-8");
 即可解决页面显示乱码问题。
 
JSP页面中
<%@ page language="java"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/encodingServlet" method="get"> //选择GET或者POST提交
 
<input type="text" name="text" > <input type="submit"> </form> </body> </html>
 
 
过滤器页面:  
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response; //放行!!!——————————————————————
chain.doFilter(newRequest, res); }
编写的servlet的get方法中:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str=request.getParameter("text");
if(str!=null)System.out.println(str);
response.getWriter().write(str);
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeoAAAB1CAYAAACBKqIYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAHYcAAB2HAY/l8WUAABIPSURBVHhe7d37c1Rlnsfx/ieS7V+c0S1mZ63BKhFqSwRm14zWbCLsCI5Ta0bLwcziUhkcNcXNgDiQsbYgspaGS5LmJgYd10YuhntClQwJCGmUS2IM6UAiBESNito/+MN3z3PO053T3eckpzsdOKTfr6pPlZx7Ou355Dl9ujsgAADAtyhqAAB8jKIGAMDHKGoAAHyMogYAwMcoagAAfIyiBgDAxyhqAAB8jKIGAMDHKGoAAHyMogYAwMcoagAAfIyiBgDAxyhqAAB8jKIGgFF27NgxaWhoIDmMekzzBUUNAKNMFUssFiM5jHpM80VGRf3WW28RQgixxYs333xTfvjhB5LDqMc0X2Rc1AAAC0V980JRu6CoAWCQ13Pi1q1b5fvvvyc5jHpM8wVFDQBZoqhvXihqFxQ1AAzKpKi/++47ksN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jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAtkf8HUxwtAV2C4MoAAAAASUVORK5CYII=" alt="" width="287" />
 
我们在过滤器中注释掉

//req.setCharacterEncoding("utf-8");
// res.setContentType("text/html;charset=utf-8");

 
控制台显示
JSP页面显示
GET
你好啊 
???
POST
你好å 
:浣犲ソ鍟�
 
在filte过滤器加上:request.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
 
 
 
控制台显示
JSP页面显示
GET
你好啊 
你好啊
POST
你好啊 
你好啊
 
 
这里JSP页面出现乱码是因为:在getParameter()方法中,得到的字符串是乱码的。
 
在Filter中:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response; //放行!!!——————————————————————
//
EnhanceRequest newRequest=new EnhanceRequest(req);
  // req.setCharacterEncoding("utf-8");
// res.setContentType("text/html;charset=utf-8");
System.out.println("filtering");
chain.doFilter(newRequest, res); }
使用装饰者模式,增强getParameter().
小插曲:  装饰者模式:
增强类与被增强类实现统一接口。
增强类中传入被增强的类
需要增强的方法重写。
之后在Filter中增强req:
class EnhanceRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public EnhanceRequest(HttpServletRequest req) {
super(req);
this.request=req;
}
//对getParameter增强
@Override
public String getParameter(String name) {
String parameter =request.getParameter(name);//乱码
try {
parameter=new String(parameter.getBytes("iso8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parameter;
} }
 
控制台显示
JSP页面显示
GET
???
???
POST
你好啊
???
 同时发现:当JSP页面中去除掉这条代码contentType="text/html; charset=UTF-8",将造成JSP页面显示数据为乱码。
结论:要解决JSP页面和控制台的乱码问题,只要在filter过滤器加上:request.setCharacterEncoding("utf-8");

        res.setContentType("text/html;charset=utf-8");
或者就在servlet页面上添加:request.setCharacterEncoding("utf-8");

        res.setContentType("text/html;charset=utf-8");
 便可解决POST和GET方法的乱码问题。
 
 
 
 
 

使用过滤器解决JSP页面的乱码问题的更多相关文章

  1. 如何解决JSP页面显示乱码问题

    一.JSP页面显示乱码 下面的显示JSP页面(display.jsp)就出现乱码: <html> <head> <title>JSP的中文处理</title& ...

  2. 如何彻底解决jsp页面中文乱码及数据库乱码

    最近自己闲做一个小项目,搭建环境框架SSH+MySQL数据库,遇到一个问题:jsp页面中文显示乱码,数据库插入数据和更新数据时中文也显示乱码,后来在网上找了许多解决方法,还是折腾了两天才把问题解决,下 ...

  3. 解决JSP页面中文乱码插入到数据库的问题

    在JSP页面使用表单注册一个用户名的时候,查看到数据库里面的表中文显示乱码的情况有两种: 1.JSP页面传进来的参数中文就是乱码,则是前台的问题,这个时候写一个过滤器就好了,可以写如下的一个过滤器 p ...

  4. jsp页面中文乱码解决方案

    一.JSP页面中文乱码 在JSP页面中,中文显示乱码有两种情况:一种是HTML中的中文乱码,另一种是在JSP中动态输出的中文乱码. 先看一个JSP程序: <%@ page language=&q ...

  5. 怎么解决JSP中出现乱码的问题

    首先我们先了解一下问题的原因.一般情况在在每个JSP页的头部都有这样一条语句: 这条语句决定了此页面使用GB2312编码形式,而在数据库中一般用的是iso-8859-1字符集存储数据. 而Java程序 ...

  6. jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题

    jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...

  7. 解决JSP页面获取的数据库数据乱码问题

    将java项目部署到服务器,页面数据乱码: 解决:首先查看了数据库编码和jsp编码都是utf-8,说明jsp和数据库没问题,于是查看了tomcat设置的编码 没有设置编码,于是加了URIEncodin ...

  8. 分享url带中文参数,打开html操作完毕跳转jsp页面中文乱码解决

    1.在app端分享参数组合时不对传递的url进行任何编码. 2.打开html页面时使用 escape函数对有中文的参数进行编码 escape(GetQueryString("paramete ...

  9. JSP页面显示乱码

    下面的显示页面(display.jsp)就出现乱码: <html> <head> <title>JSP的中文处理</title> <meta ht ...

随机推荐

  1. 用python解析word文件(一):paragraph

    太长了,我决定还是拆开三篇写.   (一)段落篇(paragraph)(本篇) (二)表格篇(table) (三)样式篇(style) 选你所需即可.下面开始正文. 最近公司的项目,需要在页面上显示w ...

  2. Jinja2 简明使用手册

    @Jinja2 简明使用手册(转载) 介绍 Jinja是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity. 运行需求 Jinja2需要 ...

  3. Linux改变文件所有者

    Linux改变文件所有者 #把当前路径下jsportal文件夹及下的所有文件的所有者改为appmanager组下的appmanager用户.chown -R -v appmanager:appmana ...

  4. Hive学习之路 (九)Hive的内置函数

    数学函数 Return Type Name (Signature) Description DOUBLE round(DOUBLE a) Returns the rounded BIGINT valu ...

  5. J - Relief grain HDU - 5029

    Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)T ...

  6. lombok问题

    今天研究了下以dubbo作为分布式的开源项目dubbo-app. 为了排除一些依赖的冲突和干扰,我另外开辟一个新的工作空间,同时我也将公司项目的依赖打个压缩包分类备份下. 这样一来,dubbo-app ...

  7. 集合之Iterator

    迭代对于我们搞Java的来说绝对不陌生.我们常常使用JDK提供的迭代接口进行Java集合的迭代. Iterator iterator = list.iterator(); while(iterator ...

  8. Qt在线/离线安装包下载网址和说明

    截至到2018年3月27日,Qt最新版本的安装需要使用在线安装工具:qt-unified-windows-x86-3.0.2-online.exe,该安装个工具的下载地址为: http://downl ...

  9. pytest 框架自动化Selenium 之yield 使用

    环境 python 3.7 由于3.0-3.5以下部分pytest可能有部分兼容问题安装建议2.7-2.9,3.5-最新 pip install pytest专属 pytest框架包 pip inst ...

  10. oracle错误分析:ora-04063:view view_test has errors

    百度了一下,有一个大佬是这样说的: 在PL/SQL中查询数据库视图时总是报告“ora-04063:view view_test has errors”的错误: Oracle视图非常强大的功能之一在于其 ...