Solved

Retriving request parameters in different languages in servlets

Posted on 2008-10-04
7
505 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now