How to display Arabic word in java server page?

亡梦爱人 提交于 2019-12-08 06:36:13

问题


I am creating a simple web application using Eclipse Kit in java. I need to display the Arabic word into a java server page.

In my web application, I am able to store the Arabic language into my database (MYSQL) and then retrieve from that database using a jdbc template (spring framework), but i could not display the Arabic word into my webpage, which means java server page.

My Environment:

  1. Java 1.7
  2. Tomcat 7.0 server
  3. Eclipse Juno version
  4. Mysql

Can anyone help me resolving this problem?


回答1:


You need to set the proper character encoding utf8 or utf16

In your JSP you need to insert the page directive for the encoding

<%@ page pageEncoding="utf-8" %>
<!DOCTYPE html>
<html>
<body>
    <h2>ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي</h2>
</body>
</html>

In a Servlet you need to set the content type in the response object

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException {
    resp.setContentType("text/plain; charset=utf-8");
    PrintWriter writer = resp.getWriter();
    writer.write("ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي");
}

Note that if you have String literals containing arabic letter then the file also has to be saved in utf8 or utf16




回答2:


you need to set http response encoding style UTF8

like...

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

Also see this article for more Reference here

try this code may help you.



来源:https://stackoverflow.com/questions/26629624/how-to-display-arabic-word-in-java-server-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!