开发者

Arabic data with jQuery-ajax and Servlets

actually i'm trying to send Arabic data using jQuery aj开发者_JS百科ax to Servlet

but when i try to reprint these data on the page it is displayed like

برÙجة

and this is my jQuery ajax code

        jQuery.ajax({
            url: "/SearchedCoursesGetter",
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            dataType: "text",
            data: {
                'searchKey':  'حديث'
            },
            success: function( data ) {
               document.write(data);
                }));
            }
        });

and this is the code in java servlet

    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF8");
    PrintWriter out = response.getWriter();
    out.print( request.getParameter("searchKey"));

any body can help me ?


Add this to the top of your JSP

<%@ page pageEncoding="UTF-8" %>

This will implicitly do response.setHeader("Content-Type", "text/html;charset=UTF-8") and response.setCharacterEncoding("UTF-8") on the JSP itself. This way the browser will interpret the document (and thus also all JS on it) as UTF-8.

You also need to ensure that your JSP/JS files are saved as UTF-8. Check the editor settings and/or Save As option, depending on the editor used.

Note that the request.setCharacterEncoding("UTF-8") has only effect on POST requests, not on GET requests. For GET requests you need to configure it at servletcontainer level. In for example Tomcat, you need to add URIEncoding="UTF-8" attribute to the <Connector> in /conf/server.xml.

See also:

  • Unicode - How to get the characters right?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜