Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retriving request parameters in different languages in servlets

Posted on 2008-10-04
7
Medium Priority
?
558 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
6 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
Technology Partners: 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

879 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