Problems with cyrillic letters in Servlet

M

Martin Fuchs

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
 
R

Roland

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_/ /__/
/ \ /_/ / \
 
M

Martin Fuchs

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");
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top