ajax problem in jsp to servlet

How do i send  non-english chars to a servlet through ajax call ?
cofactorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cofactorAuthor 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 ?
0
rrzCommented:
Maybe try,
String myparam = request.getParameter("myparam");
                   if(!(null == myparam || "".equals(myparam)))
                   {
                     myparam =  java.net.Decoder.decode(myparam , "UTF-8");
                   }  
0
cofactorAuthor Commented:
Decoder class is not present in java.net package
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

cofactorAuthor Commented:
have used   URLDecoder decode  instead

but still  printing junk chars like below

"گيلان"
0
rrzCommented:
Sorry. Yes, I meant URLEncoder.  
I don't know the solution.
0
rrzCommented:
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

0
rrzCommented:
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

0
cofactorAuthor 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");
0
rrzCommented:
You could try  
response.setContentType("text/html;charset=UTF-8");  
use it before you call getWriter .
0
cofactorAuthor 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 ?
0
rrzCommented:
>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.
0
cofactorAuthor 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




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cofactorAuthor Commented:
i want to  get  this question closed with refund.

any objection ?

@Mod , please do the needful
0
rrzCommented:
Good you have found your own solution.
>any objection ?
no
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.