Announcement Announcement Module
Collapse
No announcement yet.
Encoding problem for czech characters in HttpRequest parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Encoding problem for czech characters in HttpRequest parameters

    I have a rather nasty problem.

    I have an URL that contains an encoded string with diacritics as a parameter
    (i.e. the URL ends with something like "text=d%C3%ADt%C4%9B" - original string reads "dtě")

    I have an application based on Spring MVC.
    In the controller that handles the above-mentioned URL I want to retrieve the value of this parameter and include display it in an input textfield on a webpage.

    1) On the localhost, running Tomcat 5.5, Windows XP,
    the parameter contained in the request (that can be parsed for example from request.getQueryString() ) is in the same format as the original URL (can be seen in a browser) - "d%C3%ADt%C4%9B"

    The method request.getParameter("name") returned the text with corrupted encoding - "d*t›" so I turned to parsing the query string and using UrlDecoder to decode it. UrlDecoder.decode() method (using the utf-8 encoding) yields the correct text - i.e. "dtě".


    2) On the server, running Oracle Application Server on Linux,
    the query string returned by the request.getQueryString() method is different than on the localhost (see (1) ) - "d%edt%21".

    The method request.getParameter("name") again returned the text with corrupted encoding - this time "d?t!".

    What do I have to set-up to make this work ?? I'm really desperate - I thought this should be working. Can anyone give me a hint as to what I'm doing wrong here ??? Any help would be appreciated...

    I have a CharacterEncodingFilter set up in my web.xml as the first in line.
    What else could be necessary ??

    Code:
      <filter>
       <filter-name>Spring character encoding filter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
       </init-param>
       <init-param>
         <param-name>forceEncoding</param-name>
         <param-value>true</param-value>
       </init-param>
      </filter>

  • #2
    Please comment...

    If anyone has any comments, please post them here - maybe it's trivial or you're not sure what the problem is - please ask any additional questions, tell me I'm a total newbie or anything. I really have no clue on this one...thanx

    Comment


    • #3
      Hi

      We had some trouble with uri encoding in our project. We also use Tomcat 5.5.

      In addition to the CharacterEncodingFilter (which I assume you're using to set the encoding to utf-8) you have to configure Tomcat.

      Open tomcat/conf/server.xml and find the Connector element. Add URIEncoding="utf-8" to that element.

      Examples:

      Before:

      Code:
          <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
          <Connector port="8080" maxHttpHeaderSize="8192"
                     maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                     enableLookups="false" redirectPort="8443" acceptCount="100"
                     connectionTimeout="20000" disableUploadTimeout="true" />
      After:

      Code:
          <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
          <Connector port="8080" maxHttpHeaderSize="8192"
                     maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                     enableLookups="false" redirectPort="8443" acceptCount="100"
                     connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8" />
      This solved our problem. Hopefully, it will solve yours.

      Best regards,
      Mattias

      Comment


      • #4
        Also, make sure that both the content type and the character encoding is set in your filter. This is the filter we use (which works for us)

        Code:
        public class CharacterEncodingFilter implements Filter {
        
          private FilterConfig filterConfig = null;
        
          public void init(FilterConfig filterConfig) throws ServletException {
              this.filterConfig = filterConfig;
           }
        
           public void destroy() {
              this.filterConfig = null;
           }
        
           public void doFilter(ServletRequest request, ServletResponse response,                        FilterChain chain) throws IOException, ServletException {
             request.setCharacterEncoding("utf-8");
             response.setContentType("text/html;charset=utf-8");
             chain.doFilter(request, response);
           }
        }
        The filter and the tomcat configuration solved the problem for us. I hope it does the same for you

        Comment


        • #5
          Thanx

          Hi,

          thanx a lot for your reply! :-)

          We use Tomcat only to test our application. The production version runs on the Oracle Application Server. Maybe something similar could help there as well.

          Btw the URL is not encoded by us (the link is supplied from outside our application).

          I'll check the filter implementation, but I think that class is provided with Spring so there should be no problem there, but I'll just check anyway - who knows :-).

          Thanx again and I'll let you know of the progress...

          Comment


          • #6
            HttpServletRequest.getParameter() problem

            I found out what was causing the differences between Tomcat/WinXP and OAS/Linux - the encoding got corrupted in an URL-rewrite.

            However I still haven't solved the actual problem - which is
            that request.getParameter() returns text with corrupted encoding.
            I haven't found out what (and where) has to be set-up for this to work correctly.

            Comment

            Working...
            X