Solved

Retriving request parameters in different languages in servlets

Posted on 2008-10-04
7
540 Views
Last Modified: 2013-11-24
Hi,

I am building HTTP Interface for bulk sms messaging system using servlets as primary technology. It will receive the sms requests from clients and forward it to the Telecome bulk sms gateway. At present the servlets are able to receive request in english languages. But when i try to send parameters in other languages like hindi, marathi, arabic, chinese, etc. the servlet receives the parameters but it comes as ???? while debugging. So what is the problem here?

Code:
String accountName = request.getParameter("accountName");
String password = request.getParameter("password");

Regards,
Nirav Pradhan
0
Comment
Question by:Nirav_Pradhan
  • 3
  • 2
7 Comments
 
LVL 5

Expert Comment

by:libin_v
ID: 22643829
Use getCharacterEncoding to convert
public String getText(HttpServletRequest request)
{
  String value = request.getParameter("mytext");
 
  try{
    value = new String(value.getBytes(), request.getCharacterEncoding());
  }catch(java.io.UnsupportedEncodingException ex){
    System.err.println(ex);
  }
 
  return value;
}

Open in new window

0
 

Author Comment

by:Nirav_Pradhan
ID: 22643887
I made the changes as you said, but the got the following error which is attachted.




String accountName = new String(request.getParameter("accountName").getBytes(),request.getCharacterEncoding());
String password =  new String(request.getParameter("password").getBytes(),request.getCharacterEncoding());
 
the error code:
 
HTTP Status 500 - 
 
--------------------------------------------------------------------------------
 
type Exception report
 
message 
 
description The server encountered an internal error () that prevented it from fulfilling this request.
 
exception 
 
java.lang.NullPointerException: charsetName
	java.lang.String.<init>(String.java:442)
	java.lang.String.<init>(String.java:516)
	com.maaxsms.servlets.LoginServlet.processRequest(LoginServlet.java:40)
	com.maaxsms.servlets.LoginServlet.doGet(LoginServlet.java:85)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
 
 
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
 
 
--------------------------------------------------------------------------------
 
Apache Tomcat/6.0.16

Open in new window

0
 
LVL 5

Expert Comment

by:libin_v
ID: 22645255
if(request.getCharacterEncoding() == null)
   request.setCharacterEncoding("UTF-8");

Try this set a default CharEncoding
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Nirav_Pradhan
ID: 22645590
I tried the suggestion, this time there is no error but the parameter's value is still coming as ????.


if(request.getCharacterEncoding() == null)
    request.setCharacterEncoding("UTF-8");
 
String accountName = new String(request.getParameter("accountName").getBytes(),request.getCharacterEncoding());
String password =  new String(request.getParameter("password").getBytes(),request.getCharacterEncoding());

Open in new window

0
 
LVL 1

Accepted Solution

by:
rainmaker2k earned 500 total points
ID: 22649759
The getBytes method can also take a parameter from which encoding you're starting. As far as I know all browsers send ISO-8859-1 as their default encoding.

So, what you need to for each of those parameters is something like this:
new String(request.getParameter("accountName").getBytes("ISO-8859-1"), "UTF-8")

You might also want to play around with the UTF-8 parameter. Depending on the type of language/characterset, this might need to change.
0
 

Author Closing Comment

by:Nirav_Pradhan
ID: 31503142
Thank you very much. The last solution which you provided, it worked.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question