We help IT Professionals succeed at work.

ajax problem in jsp to servlet

cofactor
cofactor asked
on
Medium Priority
594 Views
Last Modified: 2010-04-01
How do i send  non-english chars to a servlet through ajax call ?
Comment
Watch Question

Author

Commented:
client side:
=============

In AJAX my client side is sending  spanish chars through myparam....but in server side it prints junk chars.


var url="abcd?ijkl="+escape(3)+"&myparam="+encodeURIComponent method(myparam)+"&addresstype="+escape(addresstype);

req.open("GET", url, true);
req.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
req.onreadystatechange = districtcallback;
req.send(null);





server side:
============

String myparam = request.getParameter("myparam");
                   if(!(null == myparam || "".equals(myparam)))
                   {
                           myparam = new String(myparam.getBytes("UTF-8"),"UTF-8");
                   }


               myparam prints  junk  chars




what  changes i have to do ?
rrzstudent
CERTIFIED EXPERT

Commented:
Maybe try,
String myparam = request.getParameter("myparam");
                   if(!(null == myparam || "".equals(myparam)))
                   {
                     myparam =  java.net.Decoder.decode(myparam , "UTF-8");
                   }  

Author

Commented:
Decoder class is not present in java.net package

Author

Commented:
have used   URLDecoder decode  instead

but still  printing junk chars like below

"گيلان"
rrzstudent
CERTIFIED EXPERT

Commented:
Sorry. Yes, I meant URLEncoder.  
I don't know the solution.
rrzstudent
CERTIFIED EXPERT

Commented:
I did some testing on my desktop. It works for me(IE 7 and Firefox 2) if I use ISO-8859-1 for the encoding. I don't why it doesn't work when using UTF-8.  
// test.html
--------------------
<html>
     <script type=text/javascript>
          var http = false;
          if(navigator.appName == "Microsoft Internet Explorer") {
          http = new ActiveXObject("Microsoft.XMLHTTP");
          } else { http = new XMLHttpRequest(); }
          function replace() {
                              http.open("GET", "http://localhost:8080/test.jsp", true);
                              http.onreadystatechange=function() {
                                   if(http.readyState == 4) {
                                    document.getElementById('test').innerHTML = http.responseText;
                                   }
                              }
                              http.send(null);
          }
</script>
<body>
     <A href="javascript:replace()">Replace Text</a>
     <DIV id=test>Hello</div>
</body>
</html>
----------------------------------
//test.jsp
----------------------------------------------------
<%@ page contentType="text/html;charset=ISO-8859-1"%>
Iñtërnâtiônàlizætiøn 

Open in new window

rrzstudent
CERTIFIED EXPERT

Commented:
I got to work this way(on my desktop with Tomcat)
test.html
------------------------------------
<html>
     <script type=text/javascript>
          var http = false;
          if(navigator.appName == "Microsoft Internet Explorer") {
          http = new ActiveXObject("Microsoft.XMLHTTP");
          } else { http = new XMLHttpRequest(); }
          function replace() {
                              http.open("GET", "http://localhost:8080/test.jsp?myparam=Iñtërnâtiônàlizætiøn", true);
                              http.onreadystatechange=function() {
                                   if(http.readyState == 4) {
                                    document.getElementById('test').innerHTML = http.responseText;
                                   }
                              }
                              http.send(null);
          }
</script>
<body>
     <A href="javascript:replace()">Replace Text</a>
     <DIV id=test>Hello</div>
</body>
</html>
----------------------------------
test.jsp
-------------------------------------------
<%@ page contentType="text/html;charset=UTF-8"%>
${param.myparam}  

Open in new window

Author

Commented:
><%@ page contentType="text/html;charset=UTF-8"%>

I am using  servlet  

How do i  conver the above code  into a servlet ?

is it like below or something else ?

response.setContentType("text/html");

response.setCharEncoding("UTF-8");
rrzstudent
CERTIFIED EXPERT

Commented:
You could try  
response.setContentType("text/html;charset=UTF-8");  
use it before you call getWriter .

Author

Commented:
>response.setContentType("text/html;charset=UTF-8");  

but if i want to  get the variable in the servlet and print it in the console ?

String  myparam=request.getParameter("myparam");

System.out.println(myparam)   // will  it print   Iñtërnâtiônàlizætiøn  ??


I  am asking because i dont have the system at hand right now otherwise i could test  it .

i want that  print there ......will  it come or do i need to do some decoding there ?
rrzstudent
CERTIFIED EXPERT

Commented:
>will  it come or do i need to do some decoding there ?  
The java code(Servlet)  will send the bytes. It is up to the system to use the "UTF-8" or something else.
Commented:
the way it solved is like this ..

(1)
add a  URIEncoding tag in the server.xml as below

<Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="UTF-8" />

2)use  encodeURIComponent   function in the javascript

(3)no chnage required in servelet .
 request.getParameter its will give correct value




Author

Commented:
i want to  get  this question closed with refund.

any objection ?

@Mod , please do the needful
rrzstudent
CERTIFIED EXPERT

Commented:
Good you have found your own solution.
>any objection ?
no

Explore More ContentExplore courses, solutions, and other research materials related to this topic.