我在jsp上显示UTF-8字符时遇到问题。
我在eclipse中使用eclipse,jsp文件,java类和tomcat 7 buit。 我有3个文件:
- 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;
}
}
我试图在eclipse编辑器,jsp文件,html文件等中设置UTF-8编码。 什么都行不通。 我不知道我错过了什么,但如果有人可以帮助我,我会很高兴。 谢谢, 或
答案 0 :(得分:4)
过滤器可能是解决方案,我使用了Spring的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>
如果您在显示或发送字符时遇到问题,请尝试使用jsp页面指令设置页面编码:
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
(应替换&lt;%@ page&gt;和&lt; meta&gt;标记)
答案 1 :(得分:2)
您似乎需要配置webapp以正确设置传入发布数据的字符编码。不幸的是,默认值是ISO-8859-1,tomcat坚持规范中的默认设置。您需要手动将其设置为UTF-8。这在这里解释:
http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q3
简短版本:将其添加到web.xml
应该会有所帮助:
<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>