如何将utf8与javascript、javaservlets、mysql等一起使用?
我正在尝试在我的应用程序中处理希伯来文字符。 我的应用程序构建如下:如何将utf8与javascript、javaservlets、mysql等一起使用?,javascript,mysql,servlets,utf-8,Javascript,Mysql,Servlets,Utf 8,我正在尝试在我的应用程序中处理希伯来文字符。 我的应用程序构建如下: 带有Javaservlet、jsp的ui 带有Javaservlet的服务器,mysql 我的应用程序所做的是通过UI获取数据,创建一个javascript对象,使用JSON.stringify将其转换为JSON字符串,并使用XMLHttpRequest和xhr.send(“data=“.concat(jsonString)))发送它然后JavaScript代码将jsonString发送到ui servlet,后者将其转发到服
xhr.send(“data=“.concat(jsonString)))发送它
然后JavaScript代码将jsonString发送到ui servlet,后者将其转发到服务器的servlet,后者使用hibernate api将其保存在数据库顶部
我被这个希伯来文问题困扰了一段时间,所以在研究
我要做的是:
而且
在
标记内
encodeURIComponent()
新字符串(originalString.toBytes(),“UTF8”)
调用db对象的构造函数(我使用的是hibernate),其中originalString
是可能包含希伯来文字符的字符串
xhr.overrideMimeType(“UTF-8”)
和xhr.setRequestHeader(“charset”、“UTF-8”)
,但它们没有帮助,所以我将它们注释掉“×××××”
,而不是希伯来文字符。当我尝试在UI上显示habrew字符时也是如此“Ã\u0097\u0092Ã\u0097\u0096Ã\u0097”
request.setCharacterEncoding(“UTF-8”)代码>
现在我的ui servlet中有希伯来文字符
ui servlet使用下面的代码将请求转发给服务器servlet,我在过去几个小时一直在尝试调试,但没有成功。我认为问题可能在这里:
public static String forwardToServer(String servletName ,
Map<String, Object> params ,
String encoding , String method ,
HttpSession session) {
try {
URL url = new URL(settings.LocationSettings.SERVER_ADDRESS.concat(servletName));
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
/*postData.append(URLEncoder.encode(param.getKey(), encoding));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), encoding));
*/
postData.append(param.getKey());
postData.append('=');
postData.append(String.valueOf(param.getValue()));
}
System.out.println("postData = " + postData.toString());
byte[] postDataBytes = postData.toString().getBytes(encoding);
System.out.println("postDataBytes.toString() = " + new String(postDataBytes));
byte[] postDataBytes2 = postData.toString().getBytes();
System.out.println("postDataBytes2.toString() = " + new String(postDataBytes2));
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
String mySessionCookie = "JSESSIONID="+session.getAttribute(Login.SERVER_SESSION_ID_ATT_NAME);
conn.setRequestMethod(method);
conn.setRequestProperty("Cookie", mySessionCookie);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setRequestProperty("charset" , "utf-8");
conn.setDoOutput(true);
if (postDataBytes != null && postDataBytes.length > 0) {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
bw.write(postData.toString());
bw.flush();
bw.close();
//conn.getOutputStream().write(postDataBytes);
}
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
StringBuilder sb = new StringBuilder("");
for (int c; (c = in.read()) >= 0;) {
sb.append((char)c);
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
公共静态字符串forwardToServer(字符串servletName,
映射参数,
字符串编码,字符串方法,
HttpSession(会话){
试一试{
URL URL=新URL(settings.LocationSettings.SERVER_ADDRESS.concat(servletName));
StringBuilder postData=新建StringBuilder();
对于(Map.Entry参数:params.entrySet()){
如果(postData.length()!=0)postData.append('&');
/*append(URLEncoder.encode(param.getKey(),encoding));
postData.append('=');
append(URLEncoder.encode(String.valueOf(param.getValue()),encoding));
*/
append(param.getKey());
postData.append('=');
append(String.valueOf(param.getValue());
}
System.out.println(“postData=“+postData.toString());
字节[]postDataBytes=postData.toString().getBytes(编码);
System.out.println(“postDataBytes.toString()=”+新字符串(postDataBytes));
字节[]PostDataBytes 2=postData.toString().getBytes();
System.out.println(“postDataBytes2.toString()=”+新字符串(postDataBytes2));
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
字符串mySessionCookie=“jsSessionId=“+session.getAttribute(Login.SERVER\u session\u ID\u ATT\u NAME);
conn.setRequestMethod(方法);
conn.setRequestProperty(“Cookie”,mySessionCookie);
conn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
conn.setRequestProperty(“内容长度”,String.valueOf(postDataBytes.Length));
conn.setRequestProperty(“字符集”、“utf-8”);
连接设置输出(真);
if(postDataBytes!=null&&postDataBytes.length>0){
BufferedWriter bw=新的BufferedWriter(新的OutputStreamWriter(conn.getOutputStream(),“UTF-8”);
write(postData.toString());
bw.flush();
bw.close();
//conn.getOutputStream().write(postDataBytes);
}
Reader in=new BufferedReader(新的InputStreamReader(conn.getInputStream(),encoding));
StringBuilder sb=新的StringBuilder(“”);
对于(int c;(c=in.read())>=0;){
sb.附加((char)c);
}
使某人返回字符串();
}捕获(格式错误){
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
第一个注释掉的部分(/*postData.append…..encoding))*/代码>)是我调试的一部分,并且System.out.println(“postData=“+postData.toString())代码>显示
Map<String, String[]> map = request.getParameterMap();
request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
Map<String, String[]> map = request.getParameterMap();