Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding problems in JSP

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?

like image 484
Roman Avatar asked May 31 '09 18:05

Roman


People also ask

What is <%@ in JSP?

The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code the page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page. Following is the basic syntax of page directive − <%@ page attribute = "value" %>

How does JSP handle special characters?

suppose say if you are getting data which contains special characters, and not displaying that on jsp page. so you can use the replaceAll method to display the special characters in proper format. I faced the same problem, but using this it was solved. strData = Obj.

Does Java use UTF-8?

Encoding Support in Java Java supports a wide array of encodings and their conversions to each other. The class Charset defines a set of standard encodings which every implementation of Java platform is mandated to support. This includes US-ASCII, ISO-8859-1, UTF-8, and UTF-16 to name a few.


2 Answers

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.

like image 178
erickson Avatar answered Nov 07 '22 00:11

erickson


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>
like image 36
Victor Ionescu Avatar answered Nov 06 '22 23:11

Victor Ionescu