Solved

Retriving request parameters in different languages in servlets

Posted on 2008-10-04
7
550 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

631 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