2013年10月5日星期六

Servlet To correctly display Chinese information , what to do ?

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

String title = request.getParameter("title");
String timelength = request.getParameter("timelength");

System.out.println("视频名称:" +title);
System.out.println("视频时长: "+timelength);
                response.getWriter().write(title);

}


so, I enter in the browser :
http://localhost:8080/weba/newsmanager?title =% B5% C4% B5% C4% B5% C4 & timelength = 121
( there are exceptions to a problem, I entered Chinese , so the result is converted into a string of letters , which is why it, I'm using Firefox browser )
but the console print out the information , the information displayed in the browser interface , are garbled .
I have the browser 's encoding settings into utf-8.



------ Solution ---------------------------------- ----------
console:
System.out.println("视频名称:" +new String(title.getBytes("iso-8859-1"),"utf-8"));

browser-side encoding is set to utf-8
------ For reference only ------------------------- --------------
get requests garbled, it seems you can also change the server.xml URIecoding, you try.
% B5% C4% B5% C4% B5% C4 this thing called URL encoded string , their search to learn about
------ For reference only ----------- ----------------------------
 Well connected to a three

没有评论:

发表评论