如何传递Unicode字符作为JSP / Servlet request.getParameter? [英] How to pass Unicode characters as JSP/Servlet request.getParameter?

查看:162
本文介绍了如何传递Unicode字符作为JSP / Servlet request.getParameter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的试验和错误,我仍然无法弄清楚这个问题。 JSP,servlet和数据库都设置为接受UTF-8编码,但是即使每当我对任何具有双字节字符的任何东西使用request.getParameter时,它们也会被加扰为破损字符。



我已经手动提交到数据库,它能够接受这些字符,没有问题。如果我从一个servlet中的数据库中提取文本,并以我的jsp页面的形式打印它,则显示没有问题。



我发现它作为破碎的字符,当我尝试和显示它在其他地方,使用request.getParameter检索后。



有其他人有这个问题吗?如何解决?

解决方案

如果请求和/或响应编码没有正确设置, / p>

对于GET请求,您需要在servlet容器级别配置它。目前还不清楚你使用的是哪个,但是例如Tomcat中的 URIEncoding 属性在< Connector> /conf/server.xml 中的元素。

 < Connector ... URIEncoding =UTF-8> 

对于POST请求,您需要创建



请注意,HTML < meta http-当通过HTTP投放网页时,< strong> 标签被忽略。只有当通过 file:// 从本地磁盘文件系统打开页面时才考虑。同时指定< form accept-charset> 是不必要的,因为它已默认为在使用表单提供HTML页面时使用的响应编码。另请参见 W3 HTML规范。 p>

另请参阅:




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?

解决方案

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.

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.

See also:

这篇关于如何传递Unicode字符作为JSP / Servlet request.getParameter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆