1

I want to send a Unicode to JSP page using javascript. I went throug the internet but still could not find an answer. It always shows me something like '???? ????'. Can anybody tell me the answer please.

Here is my method in controller

@RequestMapping(value = "/conv", method = RequestMethod.POST)
public @ResponseBody
String add(@RequestParam(value = "input", required = true) String input) throws    UnsupportedEncodingException {
    String result = Run.getTranslation(input);
    log.info(result);
    byte[] bytesInUTF8 = result.getBytes("UTF-8"); 
    String stringUsingUTF8 = new String(bytesInUTF8, "UTF-8"); 
    return stringUsingUTF8;
}

Here is my JavaScript code (test.js)

 $(document).ready(function(){
 $("#submit").click(function(){
  var input = $("#in").val();
  $.ajax({  
             type : "POST",   
             url : "conv",   
             data : "input=" +input,  
             //contentType: 'text/plain; charset=utf-8',
             success : function(response) {  
              $("#out").val(response)
             },  
             error : function(e) {  
              alert('Error msg: ' + e);   
             }  
        });  
    });
  $("#clear").click(function(){
  $("#out").val("");
  $("#in").val("");
  });
});

This is my JSP page

<Doctype html>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <script src="<c:url value="/resources/js/jquery.min.js"/>"></script>
   <script src="<c:url value="/resources/js/test.js"/>"></script>
<!-- You can add  resources to page like above, .css and .js should be in resources  folder  -->
</head>
<body>
    <h1>Translator</h1>
    <h4>Input</h4>
    <textarea id="in" rows="5" cols="100"></textarea>
    <br>
    <h4>Output</h4>
    <textarea id="out" rows="5" cols="100"></textarea>
    <br><br>
    <button id="clear"> Clear</button> &nbsp;&nbsp;<button id="submit"> Convert</button>    
</body>
</html>
2
  • Have you tried to encodeURIComponent your values? Commented Sep 23, 2014 at 10:13
  • Still not. Could U tell me how to do that. Coz I am new in this work. Commented Sep 23, 2014 at 10:36

2 Answers 2

2
byte[] bytesInUTF8 = result.getBytes("UTF-8"); 
String stringUsingUTF8 = new String(bytesInUTF8, "UTF-8"); 

This takes the translated Unicode String, encodes it to bytes using the UTF-8 encoding, then decodes it back to a String using the same encoding. This is a round trip, stringUsingUTF8 will always be exactly the same as result, so you can drop this.

return stringUsingUTF8;

Strings returned from Spring @ResponseBody methods get encoded for sending to the browser using the default encoding for Servlet. XMLHttpRequest expects UTF-8 by default, but because Servlet is old and horrible, that encoding is not UTF-8 but ISO-8859-1, which is almost never what you want.

To fix this you can set a custom AnnotationMethodHandlerAdapter. About this.

Note that your input is also decoded using the Servlet default of ISO-8859-1. If you need to accept non-ASCII input as well as output you will need to set a CharacterEncodingFilter in your web.xml. About this. (Note multipart POST forms and GET forms have further complications. Character encoding in Servlet is really a very sad thing.)

data : "input=" +input,

This should be encodeURIComponent(input), or just data: {input: input} to let jQuery take care of that for you. Otherwise characters like & and % will break.

Sign up to request clarification or add additional context in comments.

Comments

2

I believe you should add this to your JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

And here's some cool reading about this: http://www.joelonsoftware.com/articles/Unicode.html

3 Comments

It shows me error "The server encountered an internal error () that prevented it from fulfilling this request"
Do I have to change any configuration in Tomcat or somewhere else? This is my first time using Unicode in my applications.
The root cause of the Internal Error (HTTP 500) should be in your Tomcat's stdout/catalina.out log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.