Announcement Announcement Module
Collapse
No announcement yet.
How to set setCharacterEncoding on Request? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set setCharacterEncoding on Request?

    Hi,

    I am woking on a site that receives input in CJK.

    This may be a naive question:
    I am using org.springframework.web.servlet.DispatcherServlet as my servlet and I need to set CharacterEnconding on the HttpServletRequest.
    I looked into the source code and I relaized there is no code that calls setCharacterEncoding

    I dig into the forum and found 2 solutions:

    1. Use the CharacterEncodingFilter

    2. Override DispatcherServlet.doService as:
    public class MyServlet extends DispatcherServlet {
    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request.setCharacterEncoding( "UTF-8" );
    super.doService( request , response ) ;
    }
    }


    I tried to do #1 for it seems to be more desirable (cleaner) solution.
    in web.xml I added:
    <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>
    org.springframework.web.filter.CharacterEncodingFi lter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    But it doesn't seem to work.

    Could someone tell me how to do that? Use CharacterEncodingFilter?


    Thanks in advance.

  • #2
    Have you tried to set the forceEncoding flag of CharacterEncodingFilter? Perhaps you may want to check the url-pattern as well?

    Lawrence

    Comment


    • #3
      Are you using tomcat 5.0 ?

      moon

      Comment


      • #4
        Originally posted by yfmoan
        Are you using tomcat 5.0 ?

        moon
        yes I am using Tomcat 5.5.9 on WinXP.

        I tried with plain JSP, it works fiine...
        But not with DispatcherServlet+Freemarker view.

        Comment


        • #5
          This filter should be FIRST filter in the filter-chain.

          BTW, it's job is simply call the standard Servlets API method: request.setCharacterEncoding(this.encoding);

          This document http://java.sun.com/products/servlet...a.lang.String) says:


          Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
          So, it's obvious that this filter DOESN'T work when it is NOT the first filter in the chain. But it DOES work when this filter is the first filter defined in web.xml.

          I had the same problems, but this solution is helped.

          Comment


          • #6
            The Filter seems to be out of date. Didn't find any information about this filter in the current documentation of Spring.

            How can i now set the Encoding to UTF-8??

            Comment


            • #7
              Try the following:

              Code:
                  <filter>
                      <filter-name>SetCharacterEncodingFilter</filter-name>
                      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
                      <init-param>
                          <param-name>encoding</param-name>
                          <param-value>UTF8</param-value>
                      </init-param>
                      <init-param>
                          <param-name>forceEncoding</param-name>
                          <param-value>true</param-value>
                      </init-param>
                  </filter>
                  <filter-mapping>
                      <filter-name>SetCharacterEncodingFilter</filter-name>
                      <url-pattern>/*</url-pattern>
                  </filter-mapping>
              As earlier posters wrote, it should be your first filter.

              Cheers,
              GB
              Last edited by spiff; May 5th, 2008, 01:53 PM. Reason: Added filter mapping

              Comment


              • #8
                Ah. Ok. Thank you. I think the filter was not longer a part of spring because i can't find any information in the documentation.

                But i am was wrong. Works well. :-)

                Thank you again.

                Comment


                • #9
                  hi,

                  thanks for helping and providing to set the code.. i am using tomcat 5.0 by the way...


                  keeping posting to keep people update

                  thanks

                  Comment

                  Working...
                  X