Reputation: 209
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>
<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.
Upvotes: 0
Views: 1833
Reputation: 186
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>
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)
Upvotes: 4
Reputation: 3527
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>
Upvotes: 2