【发布时间】:2015-03-18 17:42:54
【问题描述】:
StackOverFlow 上有很多关于这个主题的问题:
- Handling Character Encoding in URI on Tomcat
- Why the character is corrupted when use request.getParameter() in java?
- request.getParameter() does not display properly character encoding in java servlet
- Character encoding problems with tomcat
Tomcat Wiki 中也有关于这个问题的链接:http://wiki.apache.org/tomcat/FAQ/CharacterEncoding
所以我不知道我应该为我的问题写什么标题。也许你会帮我解决这两个。
我遇到的问题:当带有西里尔字符的文本来自请求时,我得到以下请求参数值:to ÐÐ¸Ð·Ð½ÐµÑ Ð¸ ÑинанÑÑ。
所以我现在有什么:
- 所有
jsp文件都有 UTF-8 编码,如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- 在 Eclipse->Preferences->General->Workspace->文本文件编码设置为 UTF-8
数据库编码设置为:
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;在
\apache-tomcat-7.0.57\conf文件夹 server.xml 中,其中一个连接器被修改为:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443"
URIEncoding="UTF-8"/>
-
使用
doFilter方法创建编码过滤器:public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; String requestEncoding = request.getCharacterEncoding(); if (requestEncoding == null) { request.setCharacterEncoding(encoding); } chain.doFilter(request, response); }
从jsp 读取时,我还尝试以不同的方式获取参数,例如:
<a href="<c:url value="controller?command=viewFaculty">
<c:param name="name" value="${faculty.name}"/> </c:url>">${faculty.name}</a>
或
<a href="<c:url value="controller?command=viewFaculty"> <c:param name="name" value="${faculty.name}"/>
我仍然得到这些丑陋的字符。你能帮我提些建议吗??
【问题讨论】: