Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将utf8与javascript、javaservlets、mysql等一起使用?_Javascript_Mysql_Servlets_Utf 8 - Fatal编程技术网

如何将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,后者将其转发到服

我正在尝试在我的应用程序中处理希伯来文字符。 我的应用程序构建如下:

  • 带有Javaservlet、jsp的ui

  • 带有Javaservlet的服务器,mysql

  • 我的应用程序所做的是通过UI获取数据,创建一个javascript对象,使用JSON.stringify将其转换为JSON字符串,并使用XMLHttpRequest和
    xhr.send(“data=“.concat(jsonString)))发送它
    然后JavaScript代码将jsonString发送到ui servlet,后者将其转发到服务器的servlet,后者使用hibernate api将其保存在数据库顶部

    我被这个希伯来文问题困扰了一段时间,所以在研究

    我要做的是:

  • 我的JSP文件以

  • 而且

    标记内

  • 在javascript构造函数中,我对可能包含希伯来文字符的字段使用
    encodeURIComponent()

  • 我在UI servlet和服务器servlet过滤器上都有设置字符编码为utf-8(如果为空)

  • 我使用
    新字符串(originalString.toBytes(),“UTF8”)
    调用db对象的构造函数(我使用的是hibernate),其中
    originalString
    是可能包含希伯来文字符的字符串

  • 在persistence.xml文件中

    
    

  • 一切就绪

  • 在eclipse中,我将项目->属性->资源->文本文件编码设置为UTF8

  • 我尝试过使用
    xhr.overrideMimeType(“UTF-8”)
    xhr.setRequestHeader(“charset”、“UTF-8”)
    ,但它们没有帮助,所以我将它们注释掉

  • 我想就是这样。 我真的有种感觉,我把事情搞得一团糟

    现在,当我尝试通过ui在db上保存希伯来文字符时:

  • 当我在ui servlets上执行s.o.p时,我得到的是这样的东西:
    “×××××”
    ,而不是希伯来文字符。当我尝试在UI上显示habrew字符时也是如此

  • 当我在服务器servlets上执行s.o.p时,我会得到这样的东西:
    “Ã\u0097\u0092Ã\u0097\u0096Ã\u0097”

  • 在mysql workbench上,我看到一个,上面有标志,里面有4位数字的小正方形

  • 我非常希望能够在mysql工作台和我的UI中查看希伯来文字符

    谢谢大家!

    ------------编辑------------------

    我已经添加到我的servlet中

    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();