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() 获取中文出现乱码 问题

http请求是以ISO-8859-1的编码来传送url的 如果页面的content-type为utf-8,那么在发送请求时,会将字符转成utf-8后进行传送 如: 中 的UTF-8编码为:E4 B...

解决request.getparameter中文乱码问题。

本文是解决request.getparameter中文乱码问题。 在Java web编程中经常遇到中文乱码问题。让人一直头疼。 重点:全部统一为utf-8编码,兼容性好 解决方案分以下几种...

request.getParameter()在get和post方法中文乱码问题

乱码原因:Http请求传输时将url以ISO-8859-1编码,服务器收到字节流后默认会以ISO-8859-1编码来解码成字符流(造成中文乱码) post请求: 假设提交请求的jsp页面是UTF-8编...

request.getparameter中文乱码问题解决方案

request.getparameter中文乱码问题解决方案   百度经验:jingyan.baidu.com 本文是解决request.getparameter中文乱码问题。 ...

request.getParameter中文乱码问题

http请求是以ISO-8859-1的编码来传送url的 如果页面的content-type为utf-8,那么在发送请求时,会将字符转成utf-8后进行传送 如: 中 的UTF-8编码为:E4 B...

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

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

request.getParameter(“参数名”) 中文乱码解决方法

] 解决问题,先要研究问题,URL传中文参数为什么会出现乱码? 原因:Http请求传输时将url以ISO-8859-1编码,服务器收到字节流后默认会以ISO-8859-1编码来解码成字符流(造成...

使用request.getParameter()读取参数时出现的中文乱码现象

在服务器端用request.getParameter()读取参数时,很容易出现中文乱码现象,其中有很多种方法可以显示出中文,以下先列出一种方法: 源程序: request.getParamete...

request.getParameter(“参数名”) 中文乱码解决方法

在Java 开发中,如果框架搭建的不完善或者初学者在学习过程中,出现中文乱码是经常的事儿(哈。谁让发明java语言的不是中国人呢) 今天跟大家分享几个解决java Web开发中,request....
  • fjseryi
  • fjseryi
  • 2015年11月13日 13:01
  • 572

JSP页面数据传递中文乱码- request.getParameter()

JSP页面数据传递中文乱码- request.getParameter()
  • NJUPT_T
  • NJUPT_T
  • 2016年01月03日 22:00
  • 6381
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:request.getParameter(pName)传参中文问题
举报原因:
原因补充:

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