简体   繁体   English

Ajax到Servlet,URI编码的查询字符串

[英]Ajax to Servlet, URI encoded query string

I have a java web project running Servlet 2.4 on Apache Tomcat. 我有一个在Apache Tomcat上运行Servlet 2.4的java web项目。

In my servlet I have set request.setCharacterEncoding("utf-8") and using the <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> in HTML head tag. 在我的servlet中,我在HTML头标记中设置了request.setCharacterEncoding("utf-8")并使用<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

All files (Java, JS etc.) in the project have text file encoding set to utf-8 . 项目中的所有文件(Java,JS等)都将文本文件编码设置为utf-8 I have also added a Filter mapped to all Servlets in web.xml which sets character encoding to utf-8 . 我还添加了一个映射到web.xml中所有Servlet的Filter,它将字符编码设置为utf-8

When making ajax requests (both get and post) to the web server where I use jQuery, and the serialize method on a html form, the Servlet is unable to retrieve special utf-8 characters. 当向我使用jQuery的web服务器和html表单上的serialize方法发出ajax请求(get和post)时,Servlet无法检索特殊的utf-8字符。

Maybe it's because it expects UTF-8 and gets URLencoded string? 也许是因为它期望UTF-8并获得URLencoded字符串? Has anyone any tips regarding this? 有没有人有任何关于此的提示?

using the <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> in HTML head tag. 在HTML头标记中使用<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

Meta http-equiv tags in the HTML head are ignored when the HTML page itself is been served by a true HTTP request. 当HTML页面本身由真正的HTTP请求提供服务时,HTML头中的Meta http-equiv标签将被忽略 Instead, the information provided in HTTP response headers are been used. 而是使用HTTP响应头中提供的信息。 The meta http-equiv tags are only used when the page is not been obtained as a HTTP resource via http:// URI, such as the local disk file system via file:// URI, which may happen when the enduser saved the obtained HTML file to disk file system and reopened it from there via file explorer. meta http-equiv标签仅在未通过http:// URI作为HTTP资源获取页面时使用,例如通过file:// URI的本地磁盘文件系统,这可能在最终用户保存获取时发生HTML文件到磁盘文件系统并通过文件浏览器从那里重新打开。

You should now probably realize why the attribute is called exactly like that: http-equiv , as in "HTTP equivalent". 你现在应该知道为什么这个属性被完全调用: http-equiv ,就像在“HTTP等价物”中一样。

So, you need to set the content type and character encoding in the real HTTP response header. 因此,您需要在真实的HTTP响应头中设置内容类型和字符编码。 This can be done by placing the following line in top of JSP: 这可以通过将以下行放在JSP的顶部来完成:

<%@page pageEncoding="UTF-8"%>

Or, if you intend to apply this on all JSPs of your webapp in a single place instead of copypasting the same over all files, then put the following in webapp's web.xml : 或者,如果您打算在一个地方将所有JSP应用于您的webapp,而不是在所有文件上对其进行copypasting,那么请将以下内容放在webapp的web.xml

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

Please note that I assume that you're sending jQuery ajax requests the right way and that you're properly URI-encoding the parameters using eg $.serialize() or encodeURIComponent() . 请注意,我假设您正在以正确的方式发送jQuery ajax请求,并且您使用例如$.serialize()encodeURIComponent()正确地对参数进行URI编码。

See also: 也可以看看:

Use this contentType when execute jQuery.ajax : 执行jQuery.ajax时使用此contentType:

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

More details here: http://api.jquery.com/jQuery.ajax/ 更多细节: http//api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM