字符编码 JSP - 在 JSP 中显示错误但在 URL 中没有显示:“á » á é » é"; [英] Character encoding JSP -displayed wrong in JSP but not in URL: "á » á é » é"

查看:22
本文介绍了字符编码 JSP - 在 JSP 中显示错误但在 URL 中没有显示:“á » á é » é";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JBoss 应用服务器上运行的 JSP 中有这个 Web 应用程序.我正在使用 Servlets 来获取友好的 url.我正在通过我的 JSP 和 Servlet 发送搜索参数.我正在使用带有文本框的表单,即 Servlet

I have this Web Application in JSP running on JBoss Application Server. I am using Servlets for friendly urls. I'm sending search parameters through my JSP's and Servlets. I am using a form with a text box, the Servlet

第一个 Servlet 使用 request.getParameter() 获取文本,并使用 response.sendRedirect 将其发送到另一个 Servlet(将 URL 屏蔽为友好"的内容)).这个最终的 Servlet 使用 request.getRequestDispatcher().forward() 以丑陋"的方式将参数发送到 JSP:searchResults.jsp?searchParameters=Parameters.

The first Servlet uses request.getParameter() to get the text, and sends it to another Servlet with response.sendRedirect (masking the URL to something "friendly"). This final Servlet uses request.getRequestDispatcher().forward() to send the parameters to the JSP in the "ugly" way: searchResults.jsp?searchParameters=Parameters.

现在,当显示搜索结果"页面时,该 URL 会显示带有友好 url"的正确搜索词.示例:http://site.com/search/My-Search-Query 即使使用特殊字符,如:http://site.com/search/Busqué-tildes-y-eñies.但是当我尝试在我的 JSP 中使用该搜索词时,特殊字符无法正确显示.

Now, when the Search Results page is displayed, the URL displays the correct search term with "friendly url". Example: http://site.com/search/My-Search-Query even when using special characters like: http://site.com/search/Busqué-tildes-y-eñies. But when I try to use that search term in my JSP, the special characters are not displayed correctly.

整个系统使用 i18n,到目前为止我们没有遇到特殊字符的问题.但是当信息通过表单发送时(比如从 index.jsp 到 searchResults.jsp)特殊字符没有正确显示:

The whole system uses i18n, and we've had no problems with special characters so far. But when the information is sent through the form (say from index.jsp to searchResults.jsp) special characters are not correctly displayed:

á - á
é - é
í - Ã
ó - ó
ú - ú
ñ - ñ

整个代码库应该是 UTF-8,但显然我在传递参数时遗漏了一些东西.正如我所说,它们正确显示在 URL 中,但不在 JSP 中.

The whole code base is supposed to be in UTF-8, but apparently I'm missing something when passing the parameters. As I said, they are correctly displayed in the URL, but not inside the JSP.

我想手动转换那些 á,但我想有更好的方法来正确地做到这一点,使用正确的编码.此外,以后可能会有我现在可能不知道的新字符(法语、西班牙语等)

I was thinking of converting those á manually, but I guess there's a better way to do it correctly, using the correct encoding. Besides, there can be new characters later which I may not be aware of right now (French, Spanish, etc.)

为了以防万一,我会让你知道我在每个 JSP 上都有这些行:

Just in case, I'll let you know I have these lines on each JSP:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

编辑

感谢您的回答.我尝试了一些方法,但没有解决问题.

Thanks for your answers. I tried a few things, but nothing has fixed the problem.

这是我所做的:

  • 我添加了一个 ServletRequestListener,它将会话的字符编码设置为 UTF-8,并为每个 Http 请求添加了一个过滤器,其作用相同.

  • I added a ServletRequestListener which sets the session's character encoding to UTF-8, and a Filter for every Http request, which does the same.

正如我所说,JSP 中的所有内容都使用 UTF-8 编码(请参阅相关标头).

As I said, everything in the JSPs is encoded with UTF-8 (see headers in question).

我将 Servlet 的字符编码打印到控制台,默认情况下为 null,将它们设置为 UTF-8,就像 @kgiannakakis 和 @saua 所说的那样.

I printed the Servlets' character encoding to the console, which were null by default, set them to UTF-8 like @kgiannakakis and @saua said.

这些操作都不能解决问题.我想知道这是否还有其他问题...

None of these actions fixes the problem. I'm wondering if there's something else wrong with this...

推荐答案

尝试在 {jboss.server}/deploy/jboss-web.deployer/server.xml 中设置 URIEncoding.

Try to set URIEncoding in {jboss.server}/deploy/jboss-web.deployer/server.xml.

例如:

<Connector port="8080" address="${jboss.bind.address}"    
     maxThreads="250" maxHttpHeaderSize="8192"
     emptySessionPath="true" protocol="HTTP/1.1"
     enableLookups="false" redirectPort="8443" acceptCount="100"
     connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />

这篇关于字符编码 JSP - 在 JSP 中显示错误但在 URL 中没有显示:“á » á é » é";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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