request.getParameter(pName)传参中文问题

翻译 2016年11月22日 15:23:04

Question:

After a lot of trial and error I still can’t figure out the problem. The JSP, servlet, and database are all set to accept UTF-8 encoding, but even still whenever I use request.getParameter on anything that has any two-byte characters like the em dash they get scrambled up as broken characters.

I’ve made manual submissions to the database and it’s able to accept these characters, no problem. And if I pull the text from the database in a servlet and print it in my jsp page’s form it displays no problem.

The only time I’ve found that it comes back as broken characters is when I try and display it elsewhere after retrieving it using request.getParameter.

Has anyone else had this problem? How can I fix it?

Answer:

That can happen if request and/or response encoding isn’t properly set at all.

For GET requests, you need to configure it at the servletcontainer level. It’s unclear which one you’re using, but for in example Tomcat that’s to be done by URIEncoding attribute in <Connector> element in its /conf/server.xml.

<Connector ... URIEncoding="UTF-8">
For POST requests, you need to create a filter which is mapped on the desired URL pattern covering all those POST requests. E.g. .jsp or even /. Do the following job in doFilter():

request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);

For HTML responses and client side encoding of submitted HTML form input values, you need to set the JSP page encoding. Add this to top of the JSP (you’ve probably already done it properly given the fact that displaying UTF-8 straight form DB works fine).

<%@page pageEncoding="UTF-8" %>
Or to prevent copypasting this over every single JSP, configure it once in web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

For source code files and stdout (IDE console), you need to set the IDE workspace encoding. It’s unclear which one you’re using, but for in example Eclipse that’s to be done by setting Window > Preferences > General > Workspace > Text File Encoding to UTF-8.

enter image description here

Do note that HTML <meta http-equiv> tags are ignored when page is served over HTTP. It’s only considered when page is opened from local disk file system via file://. Also specifying <form accept-charset> is unnecessary as it already defaults to response encoding used during serving the HTML page with the form. See also W3 HTML specification.

request.getParameter() 获取中文乱码

我的第一个博客  好开心呀
  • qq_24343273
  • qq_24343273
  • 2015年06月02日 19:22
  • 893

在jsp页面传参获得String类型怎么转Date

String dateInfo=request.getParameter("birthday"); SimpleDateFormat format = new SimpleDateFormat("yy...
  • u014627365
  • u014627365
  • 2014年09月19日 00:19
  • 273

request.getParameter获取中文是乱码

检查 编码是否与项目编码一致 //订单描述 String body = new String(request.getParameter("WIDbody").getBytes("utf-8"),"...
  • baicp3
  • baicp3
  • 2014年07月02日 13:54
  • 2106

getParameter方法中文显示问号解决方法

最大的原因就是tomcat8前面的版本默认编码是ISO-8859-1,tomcat 8之后默认编码是UTF-8,所以!!!!!! 当我们在java文件中或者过滤器中添加这行代码的时候,就会变成问号!...
  • qq_35267140
  • qq_35267140
  • 2017年03月15日 19:42
  • 300

jsp页面传值取值问题request.getParameter无法取到jsp页面的值

1、首先确定 和 两不为NULL 2、再次确定 ">  和 ">  是放在form 中   在开发一个MIS系统中,部分页面中有需要上传文件的字段,相信大家在开...
  • windlyb
  • windlyb
  • 2010年07月23日 15:59
  • 9215

关于request.getParameter方法获得参数乱码问题

相信很多java初学者在学习request对象的时候会遇到一个很奇怪的问题,我明明已经设置了request.setCharacterEncoding("UTF-8");和response.setCha...
  • jiyinsui8460
  • jiyinsui8460
  • 2017年04月17日 10:29
  • 1744

request.getParameter("参数") 乱码问题

String content = new String(request.getParameter("info").getBytes("iso-8859-1"), "utf-8");
  • u012454429
  • u012454429
  • 2015年10月20日 19:27
  • 1018

Java Web后台通过request.getParameter( )方法得到的数据中文乱码

问题描述:     在“整合DataTables到JavaWeb(SSH)实例总结分析”中,发现在页面提交的中文数据,在后台会出现中文乱码 (例子链接: http://note.youdao.com/...
  • JusterDu
  • JusterDu
  • 2016年03月15日 08:44
  • 7340

web传参中文问题

服务器的默认编码为ISO-8859-11 pageEncoding="UTF-8"2 contentType="text/vnd.wap.wml;charset=utf-8"3 request.set...
  • zhaokuner
  • zhaokuner
  • 2011年03月29日 11:00
  • 813

Struts2中文传参问题

/user_preUpdate.action?user.id=${user.id}&tip=${tip} true
  • WingBin
  • WingBin
  • 2012年10月15日 21:21
  • 319
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:request.getParameter(pName)传参中文问题
举报原因:
原因补充:

(最多只允许输入30个字)