Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSP中的编码问题_Jsp_Encoding_Utf 8_Webforms - Fatal编程技术网

JSP中的编码问题

JSP中的编码问题,jsp,encoding,utf-8,webforms,Jsp,Encoding,Utf 8,Webforms,我有一个带有几个文本字段的html表单 当我尝试提交非英语字符(在我的例子中是俄语)时,服务器收到“不可读”字符串(不是问题-“??”,而是一些奇怪的字符) 我简化了代码,将其显示在此处: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html>

我有一个带有几个文本字段的html表单

当我尝试提交非英语字符(在我的例子中是俄语)时,服务器收到“不可读”字符串(不是问题-“??”,而是一些奇怪的字符)

我简化了代码,将其显示在此处:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head><title>Simple jsp page</title></head>
  <body>
    <c:out value="${param.rustext}"/>
    <form action="/test" method="post">
        <input type="text" name="rustext" width="30">
        <input type="submit" value="Submit">
    </form>
  </body>
</html>

简单jsp页面

我该如何解决这个问题

Tomcat使用ISO-8859-1作为URL参数的默认字符编码,而不考虑包含URL的页面的编码。这可以通过配置进行更改。其他应用程序服务器可能具有类似的设置


介绍了使用JSP时经常遇到的许多问题。

当使用POST时-这是必须使用的编码-表单作为内容类型“application/x-www-form-urlencoded”发送。您可以指定表单属性accept charset=“UTF-8”来指定编码。

Erickson在本页中对此做了很好的解释。独立于服务器的解决方案是使用字符编码过滤器,即org.springframework.web.filter.CharacterEncodingFilter。见下例:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
    private String encoding = "utf-8";
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        filterChain.doFilter(request, response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        String encodingParam = filterConfig.getInitParameter("encoding");
        if (encodingParam != null) {
            encoding = encodingParam;
        }
    }
    public void destroy() {
        // nothing todo
    }
}
在web.xml中,在适当的部分添加过滤器声明和过滤器url映射:

  <filter>
  <filter-name>EncodingFilter</filter-name>
  <description>
    <![CDATA[Changes the encoding of the request, in order to help the appserver to correctly interpret request params]]>
  </description>
  <filter-class>com.mypackage.EncodingFilter</filter-class>  
 <init-param>
    <param-name>encoding</param-name>
    <param-value>ISO-8859-15</param-value>
  </init-param>   
</filter>


  <filter-mapping>
      <filter-name>EncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

编码滤波器
com.mypackage.EncodingFilter
编码
ISO-8859-15
编码滤波器
/*

这与Victor Ionescu的建议相同,但您不需要编写自己的编码过滤器

将以下筛选器添加到web.xml

<filter>
    <filter-name>charsetFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>*.admin</url-pattern>
</filter-mapping>

字符集过滤器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
字符集过滤器
*管理员

您的页眉是否实际发送UTF-8作为编码?用FiddlerAnd之类的东西检查一下,如果它们不是,我该怎么办?是的,我使用Tomcat。谢谢你的链接,我将尝试在那里找到一些有用的东西。有关URIEncoding属性的详细信息,请参阅。这个答案实际上是错误的。OP使用的是POST,而不是GET。请注意,这只包括请求正文,而不是请求URL。换句话说:这将为POST请求设置编码,而不是GET请求。对于GET请求,您仍然需要修改servletcontainer的配置,正如Erickson的回答中所述。