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>
Kasun Kariyawasam
  • 2,306
  • 3
  • 21
  • 35

2 Answers2

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

the_marcelo_r
  • 1,847
  • 22
  • 35
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.

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834