UTF-8 encoding in JSP page [duplicate]

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?


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 {
   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:

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.