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

标签 java jsp servlets

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

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

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

我在第一个 jsp 上输入的值是 ÙØÙØ̄ ,它在第三个 jsp 上显示为 ÙØÙد

阿拉伯语1.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>

阿拉伯语2.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);
    }
}

最佳答案

我通过将这些行放入我的代码中解决了类似的问题:

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

关于java - 阿拉伯值在 jsp servlet 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20899607/

相关文章:

java - 为什么我收到 NullPointerException 错误?

java - 如果我在 HttpServlet#init(ServletConfig) 中分配实例字段,Servlet 规范是否保证我可以在 doGet() 中读取它们?

java - 更改java控制台输出的颜色

java - Windows 支持 Java 6 中的 native GSS-API

java - HashMap 空值问题

css - 无法在 Spring MVC 应用程序中从 CSS 访问图像文件

java - jsp中 "<%=TITLE %> "和 "${TITLE} "有什么不同?

java - 使用Ehcache缓存标签

java - 使用 JSTL/EL 将整数值转换为字符串

java - 如何在 jetty 服务器上使用 war 文件之外的文件?