Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 encoding in JSP page [duplicate]

Tags:

java

jsp

utf-8

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.

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?

like image 640
Shailendra Avatar asked Oct 04 '12 08:10

Shailendra


1 Answers

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); } 

To only set the encoding on your JSP pages, add this line to them:

<%@ page contentType="text/html; charset=UTF-8" %> 

Configure your database to use the same char encoding as well.

If you need to convert the encoding of a string see:

  • Encoding conversion in java

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.

like image 58
Jasper de Vries Avatar answered Sep 27 '22 18:09

Jasper de Vries