字符编码JSP - 在JSP中显示错误,但不在URL中:“á»Ã¡é»Ã©” [英] Character encoding JSP -displayed wrong in JSP but not in URL: "á » á é » é"

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

问题描述

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



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



现在,当显示搜索结果页面时,该网址将显示具有友好网址的正确搜索字词。例如: http://site.com/search/My-Search-Query 即使使用特殊字符,如: http://site.com /搜索/Busqué-波浪号-Y-eñies。但是,当我尝试在JSP中使用该搜索字词时,特殊字符无法正确显示。



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



á - áÃÃÃÃà - - - ÃÃÃÃÃÃÃà - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Ã

ó - ó

ú - ú

ñ - Ã¥



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



我正在考虑转换那些¡ c $ c>手动,但我想有一个更好的方法来正确地使用正确的编码。此外,以后可能会有新的角色,我现在可能不知道(法语,西班牙语等)。



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



<?xml version =1.0encoding =UTF-8?& code>

<%@ page language =javacontentType =text / html; charset = UTF-8pageEncoding =UTF-8% < / code>



编辑



感谢您的答案。我尝试了一些事情,但没有任何事情解决了这个问题。



这是我所做的:




  • 我添加了一个ServletRequestListener,它将会话的字符编码设置为UTF-8,并为每个Http请求提供一个Filter。


  • 正如我所说,JSP中的所有内容都使用UTF-8编码(参见有问题的标题)。


  • 我将Servlets的字符编码打印到控制台,默认情况下为空,将其设置为UTF-8,如@kgiannakakis和@saua说。




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

解决方案

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



例如:

 < Connector port =8080address =$ {jboss.bind.address}
maxThreads =250maxHttpHeaderSize =8192
emptySessionPath =trueprotocol =HTTP /1.1
enableLookups =falseredirectPort =8443acceptCount =100
connectionTimeout =20000disableUploadTimeout =trueURIEncoding =UTF-8/>


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

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.

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.

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:

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

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.)

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"%>

EDIT

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

Here's what I've done:

  • 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.

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

  • 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...

解决方案

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

Ex:

<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天全站免登陆