【问题标题】:Tomcat encoding issue againTomcat编码问题再次出现
【发布时间】:2015-03-18 17:42:54
【问题描述】:

StackOverFlow 上有很多关于这个主题的问题:

  1. Handling Character Encoding in URI on Tomcat
  2. Why the character is corrupted when use request.getParameter() in java?
  3. request.getParameter() does not display properly character encoding in java servlet
  4. Character encoding problems with tomcat

Tomcat Wiki 中也有关于这个问题的链接:http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

所以我不知道我应该为我的问题写什么标题。也许你会帮我解决这两个。

我遇到的问题:当带有西里尔字符的文本来自请求时,我得到以下请求参数值:to ÐÐ¸Ð·Ð½ÐµÑ Ð¸ ÑинанÑÑ

所以我现在有什么:

  1. 所有 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">
  1. 在 Eclipse->Preferences->General->Workspace->文本文件编码设置为 UTF-8
  2. 数据库编码设置为:DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

  3. \apache-tomcat-7.0.57\conf 文件夹 server.xml 中,其中一个连接器被修改为:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

  1. 使用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}"/>

我仍然得到这些丑陋的字符。你能帮我提些建议吗??

【问题讨论】:

    标签: java jsp tomcat encoding


    【解决方案1】:

    我解决了我的问题。问题是我编辑的不是原始的server.xml。我注意到tomcat 已经出现在Eclipse 项目资源管理器中,作为名为@9​​87654324@ 的项目的书签。 还有一些文件在他里面,比如:

    1. catalina.policy
    2. catalina.properties
    3. context.xml
    4. web.xml
    5. tomcat-users.xml
    6. server.xml

    所以碰巧tomcat已经有预配置的文件,奇怪的是我在\apache-tomcat-7.0.57\conf文件夹中编辑的文件server.xml与这个项目中的不同。

    在问题的开头,我已经将一些答案与这个问题联系起来,在我的帖子结束时,我想分享我最近发现的一个,在我看来这是解决这个问题的最佳指南:@987654321 @

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      相关资源
      最近更新 更多