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->Preferances->General->Workspace->Text files encoding set to 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}"/>
而且我仍然得到这些丑陋的字符。你能帮我提点建议吗??
最佳答案
我解决了我的问题。问题是我编辑的不是原始的 server.xml
。我注意到 tomcat
已作为名为 Servers
的项目的书 checkout 现在 Eclipse 项目资源管理器中。
还有一些文件就在他里面,比如:
catalina.policy
catalina.properties
context.xml
web.xml
tomcat-users.xml
和server.xml
所以碰巧tomcat已经有预配置的文件,奇怪的是我自己编辑了\apache-tomcat-7.0.57\conf
文件夹中的文件server.xml
与该项目中的不同。
在问题的开头,我已经将一些答案与这个问题联系起来,在我的帖子的结尾,我想分享我最近发现的一个,在我看来这是解决这个问题的最佳指南:How to get UTF-8 working in Java webapps?
关于java - 再次出现Tomcat编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28046307/