UTF-8 encoding in JSP page [duplicate]

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.

Leave a Comment

tech