Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

tomcat - Character encoding JSP -displayed wrong in JSP but not in URL: "á » á é » é"

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:

á - ??
é - ??
í - ?
ó - ?3
ú - ?o
? - ?±

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...