我需要在我的Java webapp(servlets + JSP,没有使用框架)中使用UTF-8来支持äöå常规芬兰语文本和???特殊情况下的西里尔字母.
我的设置如下:
使用的数据库:MySQL 5.x.
用户主要使用Firefox2,但Opera 9.x,FF3,IE7和谷歌Chrome也用于访问该网站.
怎么做到这一点?
我正在使用Spring MVC的字符集过滤器.这是我用来从applet调用servlet的URL
如您所见,该参数具有unicode字符団.所以我用
URLEncoder.encode("?", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
现在我的网址变成了
但是,从servlet调用
request.getParameter("p1");
Run Code Online (Sandbox Code Playgroud)
已经返回一些无法解码的乱码URLDecoder.BTW,调用
URLDecoder.decode("%E5%9B%A3", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
确实给了我原始的unicode角色.只是servlet在甚至可以解码之前使参数乱码.有谁知道为什么?request.getParameter()不使用UTF-8解码参数?
如何使servlet接受从JSP传递的非ascii(阿拉伯语,中文等)字符?
我试图将以下内容添加到JSP的顶部:
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Run Code Online (Sandbox Code Playgroud)
并在servlet中的每个post/get方法中添加以下内容:
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Run Code Online (Sandbox Code Playgroud)
我试图添加一个Filter来执行上面两个语句而不是servlet.
说实话,过去这些都在起作用,但现在它不再起作用了.
我在Win和Linux机器上的JDK1.6上使用tomcat 5.0.28/6.xx.
这是一个例子:JSP页面:
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>Push Engine</title>
</head>
<body>
Hello ${requestScope['val']}
<form action="ControllerServlet" method="POST">
<table>
<tr>
<td>ABC</td>
<td><input name="ABC" type="text" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Servlet doGet方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String val = "request.getParameter('ABC') : " + request.getParameter("ABC");
System.out.println(val);
request.setAttribute("val", val);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
Run Code Online (Sandbox Code Playgroud)
问题是: 在控制台中,值"???" 然而,正在打印的值返回到包含正确Unicode字的JSP页面
"???" …
我正在使用spring mvc.我正在冲浪:
http://localhost:8080/services/cities/??.html
Run Code Online (Sandbox Code Playgroud)
通知??是希伯来语而不是英语.
我的控制器是:
@RequestMapping(value="/services/cities/{name}", method=RequestMethod.GET)
public @ResponseBody List<SelectElement> getCities(@PathVariable String name) {
List<SelectElement> elements=null;
...
...
return elements;
}
Run Code Online (Sandbox Code Playgroud)
问题是控制器接收פת而不是正确的字符.
我该如何解决?
即使我冲浪:http://localhost:8080/services/cities/%D7%A4%D7%AA.html我遇到了这个问题.