Encoding problems in JSP

2019-04-25 12:13发布

问题:

I have an html-form with several text fields.

When I try to submit not English characters (Russian in my case) server is received "unreadable" string (not questions - "???" but some strange characters).

I simplified my code to show it here:

<%@ 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>

How should I fix that?

回答1:

Tomcat uses ISO-8859-1 as the default character encoding for URL parameters, regardless of the encoding of the page that contains the URL. This can be changed with the "URIEncoding" attribute in its Connector configuration. Other application servers may have similar settings.

This article covers many problems commonly encountered when working with JSP.



回答2:

Erickson explained this very well on this page. A server-independent solution is to use a character encoding filter, à la org.springframework.web.filter.CharacterEncodingFilter. See example below:

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
    }
}

In web.xml add the filter declaration and the filter url mapping in the appropriate sections:

  <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>


回答3:

When using POST - which is a must tu use encoding - the form is send as content-type "application/x-www-form-urlencoded". You may specify the form-attribute accept-charset="UTF-8" to specify your encoding.



回答4:

This is the same as Victor Ionescu proposed, but you don't need to write your own encoding filter.

Add the following filter to 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>