0

This is my Jsp page:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
 <body>
<%
String msg="206_John_help i m in trouble,delhi,อินเดีย_30.64741430_76.817313799";
String result = java.net.URLEncoder.encode(msg, "UTF-8");
System.out.println("The msg is "+result);
String result1=java.net.URLDecoder.decode(result, "UTF-8");
System.out.println("The decoded msg is "+result1);

%>
</body>
</html>

The output is 206_John_help i m in trouble,delhi,???????_30.64741430_76.817313799

I am always getting ?????? instead of thai alphabets. How can I get the Thai alphabets while decoding?

AK2
  • 111
  • 2
  • 13
  • try to write `hello.html` that contains Thai characters, put it under file system and tomcat and try to open it with your browser. Can you see Thai characters correctly in this case? – AlexR Nov 27 '14 at 09:33

3 Answers3

2

You can try adding this at the top:

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

I think this will solve the problem. If not, could you give us more information?

Are you using eclipse? If yes, are you developing under windows? In windows, eclipse by default works with WINDOWS CP1252 codification for jsp and other files. Probably the text written can be CP1252 charset.

Where are running tomcat and glashfish? Windows? Linux?

The_Reaper
  • 157
  • 10
  • I am running glassfish as a local server but apache tomcat as online server.No I am not using eclipse I am using netbeans.The above solution didn't work.I am using windows – AK2 Nov 27 '14 at 09:52
0
 Try to call these 2 methods before encoding and decoding.  
 String msg="206_John_help i m in trouble,delhi,อินเดีย_30.64741430_76.817313799";
 String result = java.net.URLEncoder.encode(convertFromUTF8(msg), "UTF-8");
 System.out.println("The msg is "+result);
 String result1=java.net.URLDecoder.decode(convertToUTF8(result), "UTF-8");
 System.out.println("The decoded msg is "+result1);     
 /* convert from UTF-8 encoded HTML-Pages -> internal Java String Format */
 public static String convertFromUTF8(String s) {
 String out = null;
 try {
 out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
 } catch (java.io.UnsupportedEncodingException e) {
 return null;
 }
 return out;
 }

 /* convert from internal Java String Format -> UTF-8 encoded HTML/JSP-Pages  */
 public static String convertToUTF8(String s) {
 String out = null;
 try {
 out = new String(s.getBytes("UTF-8"));
 } catch (java.io.UnsupportedEncodingException e) {
 return null;
 }
 return out;
 }
Vishu
  • 29
  • 10
  • define these methods inside <%! %> and call it anywhere in your jsp – Vishu Nov 27 '14 at 09:57
  • It is giving exception Compiling 1 source file to C:\Users\Java\Documents\NetBeansProjects\demos\build\generated\classes C:\Users\Java\Documents\NetBeansProjects\demos\build\generated\src\org\apache\jsp\new_0020jsp1_jsp.java:57: error: cannot find symbol NewClass1 nw=new NewClass1(); ^ symbol: class NewClass1 location: class new_0020jsp1_jsp C:\Users\Java\Documents\NetBeansProjects\demos\build\generated\src\org\apache\jsp\new_0020jsp1_jsp.java:57: error: cannot find symbol NewClass1 nw=new NewClass1(); ^ symbol: class NewClass1 – AK2 Nov 27 '14 at 10:05
  • where come you get NewClass. Either you pass the full code or tell the details. The solution I gave you works, and it only needs when you have not configured your servers to UTF-8. Either go to server. xml and changed the connector to accept UTF-8. – Vishu Nov 27 '14 at 11:15
0

Could you check the jsp charset codification in netbeans?. I think this is the problem. Probably the JSP is compiled in windows charset, this means that msg string value will complied in windows charset, then texts written in jsp will codified in this charset.

Try to configure netbeans to compile jsp in utf-8 charset instead windows cp1252 and try again.

The_Reaper
  • 157
  • 10
  • How to check the the jsp charset codification in netbeans? – AK2 Nov 27 '14 at 10:56
  • Try the solution described in this link (First Answer) http://stackoverflow.com/questions/4900157/saving-jsp-as-utf-8-in-netbeans – The_Reaper Nov 27 '14 at 11:15