0

I'm having a problem to display UTF-8 characters on jsp.

I'm using eclipse, jsp file, java class and tomcat 7 buit in the eclipse. I have 3 files :

 - index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="getProperties.jsp" method="post" name="GettingMovieParameters">
        Choose your name : <input type="text" name="hebrewMovieName"> 
        <input type="submit" value="send">
        </form>
    </body>
    </html>
  • getProperties.jsp

<jsp:useBean id="propertiesBean" class="my.movieDownloader.org.UserData" scope="session"/>
<jsp:setProperty name="propertiesBean" property="*"/> 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    Movie hebrew name is : <%= propertiesBean.getHebrewMovieName() %>
</body>
</html>



  UserData.java

    public class UserData {
    private String hebrewMovieName;

    public String getHebrewMovieName() {
        return hebrewMovieName;
    }
    public void setHebrewMovieName(String hebrewMovieName) {
        System.out.println("Setting up: "  +hebrewMovieName);
        this.hebrewMovieName = hebrewMovieName;
    }
}

I tried to set UTF-8 encoding in eclipse editor, the jsp files, html files, etc. And nothing works. I don't know what I'm missing but I'll be happy if someone can help me.. Thanks, Or.

ork
  • 209
  • 3
  • 9
  • 17

2 Answers2

4

server side

filter may be the solution, I used Spring's CharacterEncodingFilter:

  <filter>
    <filter-name>CharacterEncodingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  ...
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

client side

if you're having trouble displaying or sending characters try using the jsp page directive to set the pages encoding:

<jsp:directive.page contentType="text/html;charset=UTF-8"/>

(should replace both <%@page> and <meta> tags)

orikosaki
  • 176
  • 1
  • 6
2

It seems you need to configure the webapp to set the characher encoding of the incoming post data correctly. Unfortunately the default is here ISO-8859-1, and tomcat sticks to the default set up in the specification. You need to set it to UTF-8 manually. This is explained here:

http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q3

Short version: adding this to the web.xml should help:

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Clemens Klein-Robbenhaar
  • 3,457
  • 1
  • 18
  • 27
  • I tried it with no success. I'm using Tomcat 7, and i did it.. but still i have the same problem. – ork Feb 16 '13 at 20:42
  • my answer works only for post request; I hope I did not cause this to mess up because I asked if it works for get in a comment. For get-requests there is a different configuration tweak to be made in the `server.xml` by adding a `URIEncoding="UTF-8"` to the ` – Clemens Klein-Robbenhaar Feb 16 '13 at 21:58
  • With the get request your solution worked! Thanks alot!! Do you have another idea to the post? – ork Feb 17 '13 at 19:16
  • I am completely confused. The filter should only work for post-requests, and the `URIEncoding="UTF-8"` only works for get-requests. So adding the filter and then having get working is something I cannot explain. – Clemens Klein-Robbenhaar Feb 17 '13 at 19:21
  • No.. I tried to use the URIEncoding="UTF-8" with get request and it worked. The filter doesn't work for me with the post request.. Do you have any idea to resolve the post issue? – ork Feb 17 '13 at 20:41
  • I forgot to mention that the `filter` needs a `filter-mapping` to be active. I have updated my answer. Please try the updated answer, and please note that the `filter-mapping` for the `CharacterEncodingFilter` must be the first filter-mapping in the `web.xml` – Clemens Klein-Robbenhaar Feb 17 '13 at 21:04