cancel
Showing results for 
Search instead for 
Did you mean: 

Character Encoding Problem RequestDispatcher

former_member187444
Participant
0 Kudos

Hi Friends;

I have a servlet and I use

RequestDispatcher dispatcher = request.getRequestDispatcher(page);
			response.setContentType("text/html;charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			//request.setCharacterEncoding("utf-8");
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

for forward page. But if i forward a page, this pages' encoding isnot working. But if i refresh page it works.

How can i handle this encoding.

Thanks replies

Accepted Solutions (1)

Accepted Solutions (1)

Sigiswald
Contributor
0 Kudos

Hi Eray,

It really doesn't make any sense (exept in very specific, well, exceptional cases) to call setContentType and/or getWriter on the response object before doing a forward (neither does calling setCharacterEncoding on the request object usually makes sense);

request.getRequestDispatcher(page).forward(request, response);

should be sufficient. I assume you forward to a jsp page? In case you want to enforce the response encoding of the content of the jsp page to be UTF-8, you can use this page directive at the top of your jsp page:

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

I don't know why it does work when you refresh the page, maybe your browser configuration is not correct (in IE: Page/View > Encoding > Auto-Select)?

Kind regards,

Sigiswald

Answers (2)

Answers (2)

former_member187444
Participant
0 Kudos

thanks for your reply but it didnt work

Former Member
0 Kudos

Hi,

Try the below code


RequestDispatcher dispatcher = request.getRequestDispatcher(page);
			response.setContentType("text/html");
			request.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			//request.setCharacterEncoding("utf-8");
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

Regards,

Beevin.