Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retriving request parameters in different languages in servlets

Posted on 2008-10-04
7
Medium Priority
?
555 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

688 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