Problems with cyrillic letters in Servlet

Discussion in 'Java' started by Martin Fuchs, Mar 23, 2005.

  1. Martin Fuchs

    Martin Fuchs Guest

    Hi,

    I want to handle multilingual user entrys (english, cyrillic, german)
    from a HTML-Form in a Servlet. The Servlet-Container is Tomcat5 with
    Axis.

    The HTML looks something like this:
    ----------------------------------------------------------
    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    ....
    <body>
    <form method="post" action="/axis/servlet/ObjectSearchServlet"
    target="OBJ_SEARCH_RESULT" accept-charset="UTF-8">
    ....
    <input name="test"...>
    ....
    <form>
    </body>
    </html>
    ----------------------------------------------------------

    At the beginning of the "doPost"-method in the Servlet I set the
    character encoding of the request to utf-8:
    ----------------------------------------------------------
    request.setCharacterEncoding("UTF-8");
    ----------------------------------------------------------

    Later I fetch the parameter from the request and write the string to
    System.out
    ----------------------------------------------------------
    string = request.getParameter("test");
    System.out.println(string);
    ----------------------------------------------------------

    Everything works fine with german or english letters, but the cyrillic
    letters are "trash".

    I spent several hours on reading web-sites an postings and tried a lot
    of different ways, but it didn't work.

    Does anyone no what the problem could be?

    Best regards,
    Martin
     
    Martin Fuchs, Mar 23, 2005
    #1
    1. Advertising

  2. Martin Fuchs

    Roland Guest

    On 23-3-2005 16:47, Martin Fuchs wrote:

    > Hi,
    >
    > I want to handle multilingual user entrys (english, cyrillic, german)
    > from a HTML-Form in a Servlet. The Servlet-Container is Tomcat5 with
    > Axis.
    >
    > The HTML looks something like this:
    > ----------------------------------------------------------
    > <html>
    > <head>
    > <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    > ....
    > <body>
    > <form method="post" action="/axis/servlet/ObjectSearchServlet"
    > target="OBJ_SEARCH_RESULT" accept-charset="UTF-8">
    > ....
    > <input name="test"...>
    > ....
    > <form>
    > </body>
    > </html>
    > ----------------------------------------------------------
    >
    > At the beginning of the "doPost"-method in the Servlet I set the
    > character encoding of the request to utf-8:
    > ----------------------------------------------------------
    > request.setCharacterEncoding("UTF-8");
    > ----------------------------------------------------------
    >
    > Later I fetch the parameter from the request and write the string to
    > System.out
    > ----------------------------------------------------------
    > string = request.getParameter("test");
    > System.out.println(string);
    > ----------------------------------------------------------
    >
    > Everything works fine with german or english letters, but the cyrillic
    > letters are "trash".
    >
    > I spent several hours on reading web-sites an postings and tried a lot
    > of different ways, but it didn't work.
    >
    > Does anyone no what the problem could be?
    >
    > Best regards,
    > Martin


    Maybe the font used for the terminal output (System.out.println())
    doesn't support glyphs for cyrillic characters. Verify if you receive
    the characters by printing the unicode of each character in the string
    (i.e. the request parameter "test").
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Mar 24, 2005
    #2
    1. Advertising

  3. Martin Fuchs

    Martin Fuchs Guest

    Hi Roland,

    thank you for your answer, but I solved the problem in the meantime:

    The character encoding of the request has to be set before any
    "request.getParameter(...)" directive. In my servlet there was such a
    directive to get the "action" parameter before the
    "request.setCharacterEncoding("UTF-8")".

    Setting the character encoding later doesn't work!

    Wrong Example:
    ------------------------------------------------------------
    ....
    //Check which action to perform
    String action = null;
    action = request.getParameter("action");
    ....
    // set character encoding
    request.setCharacterEncoding("UTF-8");
    ....
    String cyrillic = null;
    cyrillic = request.getParameter("test");
    ------------------------------------------------------------

    Correct Example:
    ------------------------------------------------------------
    ....
    // set character encoding
    request.setCharacterEncoding("UTF-8");
    ....
    //Check which action to perform
    String action = null;
    action = request.getParameter("action");
    ....
    String cyrillic = null;
    cyrillic = request.getParameter("test");
    ------------------------------------------------------------

    Best regards,

    Martin Fuchs
     
    Martin Fuchs, Mar 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Milsnips
    Replies:
    0
    Views:
    505
    Milsnips
    Oct 13, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    2,094
    Jack Jia
    Apr 4, 2004
  3. Crouchinho
    Replies:
    2
    Views:
    439
    Crouchinho
    Sep 27, 2006
  4. Merrigan
    Replies:
    4
    Views:
    611
    Chris
    Dec 14, 2007
  5. Venugopal
    Replies:
    11
    Views:
    1,686
    Tassilo v. Parseval
    Nov 5, 2003
Loading...

Share This Page