阿拉伯语值在jsp servlet中不起作用

时间:2014-01-03 08:34:19

标签: java jsp servlets

我有一个演示代码,其中有三个名为arabic1.jsparabic2.jsparabic3.jsp的jsp和一个名为ArabicServlet的servlet。

arabic1.jsp内我有一个文本框,我在其中输入了阿拉伯语值并通过调用ArabicServlet servlet提交了页面,该文件将相同的内容转发到arabic2.jsp,其中我在其中显示了相同的内容正确显示的文本框。

现在我在arabic2.jsp上有一个链接使用相同的servlet和内容转发arabic3.jsp上的请求,arabic3.jsp我在文本框中显示了相同的阿拉伯语内容,但我的字符错误。

我在第一个jsp上输入值محمد,它在第三个jsp显示为ÙØÙد

arabic1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
    <html>
        <head>
        </head>
        <body>
            <form action="/test/ArabicServlet" method="post">
                <table name="tbl1" id="tbl1">
                    <tr>
                        <td>
                            <input type="hidden" id="page" name="page" value="1"/>
                        </td>
                        <td>
                        Name :
                        </td>
                        <td>
                            <input type="text" id="arabic" name="arabic">
                        </td>
                        <td>
                            <input type="submit" id="arabic" name="arabic">
                        </td>
                    </tr>
                </table>
            </form>
        </body>
    </html>

arabic2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
        <head>
            <%
            Map map = (Hashtable)request.getAttribute("arabic");
            String arabicvalue = (String)map.get("arabicmap");
            System.out.println("Arabic Value on jsp = "+arabicvalue);
            String url = "/test/ArabicServlet?page=2&arabicvalue="+arabicvalue;
            %>
        </head>
        <body>
            <form action="/test/ArabicServlet" method="post">
                <table name="tbl1" id="tbl1">
                    <tr>
                        <td>
                            Name : 
                        </td>
                        <td>
                            <input type="text" id="arabic" name="arabic" value="<%=arabicvalue%>">
                        </td>
                        <td>
                            <a href="<%=url%>"> Test </a>
                        </td>
                    </tr>
                </table>
            </form>
        </body>
    </html>

arabic3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
    <html>
        <head>
            <%
            Map map = (Hashtable)request.getAttribute("arabic");
            String arabicvalue = (String)map.get("arabicmap");
            System.out.println("Arabic Value on jsp = "+arabicvalue);
            String url = "/test/ArabicServlet?page=2&arabicvalue="+arabicvalue;
            %>
        </head>
        <body>
            <form action="/test/ArabicServlet" method="post">
                <table name="tbl1" id="tbl1">
                    <tr>
                        <td>
                            Name : 
                        </td>
                        <td>
                            <input type="text" id="arabic" name="arabic" value="<%=arabicvalue%>">
                        </td>   
                    </tr>
                </table>
            </form>
        </body>
    </html>

ArabicServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException 
{
    request.setCharacterEncoding("UTF-8");
    String page = request.getParameter("page");
    System.out.println("Page Number = "+page);

    if(page.equals("1")){
        String arabic = (String)request.getParameter("arabic");
        System.out.println("Arabic Value from first page = "+arabic);

        Map map = new java.util.Hashtable();
        map.put("arabicmap",arabic);
        request.setAttribute("arabic",map);
        request.getRequestDispatcher("arabic2.jsp").forward(request,response);
    }else{
        String arabic = (String)request.getParameter("arabicvalue");
        Map map = new java.util.Hashtable();
        map.put("arabicmap",arabic);
        request.setAttribute("arabic",map);
        System.out.println("Arabic Value from first page = "+arabic);
        request.getRequestDispatcher("arabic3.jsp").forward(request,response);
    }
}

1 个答案:

答案 0 :(得分:0)

我通过在代码中添加以下行来解决类似的问题:

  request.setCharacterEncoding("UTF-8");
  response.setCharacterEncoding("UTF-8");