How can I fix the encoding encoding when I use RequestDispatcher includes html file?

I have a login and registration of a dynamic web project using JSP and Servlet.

register.htm aready has:
<meta charset="UTF-8">

Servl register has:

    `response.setContentType("text/html; charset=UTF-8");
    `response.setCharacterEncoding("UTF-8");`
    `response.setHeader("Cache-Control", "no-cache");`
    `request.setCharacterEncoding("UTF-8");`

      

When the user submits (to register.html), if he has some errors, register the Servlet:

RequestDispatcher rd = getServletContext().getRequestDispatcher("/register.html"); PrintWriter out = response.getWriter(); out.println("<font color=red>" + errorsMsg + "</font>"); rd.include(request, response);


Then display browser content that is not UTF8. (Like Ä á "ƒ Ä'ăng ký tà i khoẠ£ n, bạn cần Ä'á" "ng ý vá"> i
How can I fix this?

+3


source to share


1 answer


You must set all HTTP headers before starting to write to the output stream or write the response.

And when you start writing to the writer, you must first write the <HTML>

and tags <HEAD>

, but in your example, you write <font>

:



PrintWriter out = response.getWriter();
out.println("<font color=red>" + errorsMsg + "</font>");

      

When you call rd.include(request, response);

, you've already written something to the output that you don't need.

0


source







All Articles