JSP页面中的UTF-8编码 [英] UTF-8 encoding in JSP page
问题描述
我有一个JSP
页面,其页面编码为ISO-8859-1
.这个JSP页面在问答博客中.我想在Q/A发布期间添加特殊字符.
I have a JSP
page whose page encoding is ISO-8859-1
. This JSP page there is in a question answer blog. I want to include special characters during Q/A posting.
问题在于,即使我将其从ISO-8859-1
更改为UTF-8
,JSP也不支持UTF-8
编码.这些字符(~
,%
,&
,+
)出现问题.当我单独或与任何字符组合发布这些字符时,它将存储在数据库中null
,并且在发布应用程序时删除这些字符时,它可以正常工作.
The problem is JSP is not supporting UTF-8
encoding even I have changed it from ISO-8859-1
to UTF-8
. These characters (~
,%
,&
,+
) are making problem. When I am posting these character either individually or with the combination of any character it is storinh null
in the database and when I remove these characters while posting application it is working fine.
任何人都可以提出一些解决方案吗?
Can any one suggest some solution?
推荐答案
您应在应用程序的所有层上使用相同的编码,以避免此问题.添加过滤器以设置编码很有用:
You should use the same encoding on all layers of your application to avoid this problem. It is useful to add a filter to set the encoding:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
要仅在JSP页面上设置编码,请向其添加以下行:
To only set the encoding on your JSP pages, add this line to them:
<%@ page contentType="text/html; charset=UTF-8" %>
将您的数据库配置为也使用相同的char编码.
Configure your database to use the same char encoding as well.
如果您需要转换字符串的编码,请参见:
If you need to convert the encoding of a string see:
我不建议在您的数据库中存储HTML编码的文本.例如,如果您需要生成PDF(或HTML以外的任何其他文件),则需要先转换HTML编码.
I would not recommend to store HTML encoded text in your database. For example, if you need to generate a PDF (or anything other than HTML) you need to convert the HTML encoding first.
这篇关于JSP页面中的UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!