我有一个演示代码,其中有三个名为 arabic1.jsp
、arabic2.jsp
、arabic3.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/