dev@glassfish.java.net

Re: error-page encoding problem

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Fri, 08 Dec 2006 09:22:35 -0800

Hi Dmitry,

Dmitry Mozheyko wrote On 12/07/06 22:36,:

>-----Original Message-----
>From: Jan Luehe <Jan.Luehe_at_Sun.COM>
>To: dev_at_glassfish.dev.java.net
>Date: Thu, 07 Dec 2006 09:22:46 -0800
>Subject: Re: error-page encoding problem
>
>
>
>>Hi Dmitry,
>>
>>Dmitry Mozheyko wrote On 12/06/06 22:21,:
>>
>>
>>
>>>-----Original Message-----
>>>From: Jan Luehe <Jan.Luehe_at_Sun.COM>
>>>To: dev_at_glassfish.dev.java.net
>>>Date: Wed, 06 Dec 2006 14:55:18 -0800
>>>Subject: Re: error-page encoding problem
>>>
>>>
>>>
>>>
>>>
>>>>Hi Dmitry,
>>>>
>>>>Dmitry Mozheyko wrote On 12/06/06 02:54,:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>>Hi all!
>>>>>I use glassfish-v2-b27 and i need to custom error page
>>>>>web.xml:
>>>>>
>>>>>...
>>>>> <error-page>
>>>>> <exception-type>java.lang.Throwable</exception-type>
>>>>> <location>/error.jsp</location>
>>>>> </error-page>
>>>>> <error-page>
>>>>> <error-code>403</error-code>
>>>>> <location>/error403.jsp</location>
>>>>> </error-page>
>>>>> <error-page>
>>>>> <error-code>404</error-code>
>>>>> <location>/error404.jsp</location>
>>>>> </error-page>
>>>>>...
>>>>>
>>>>>
>>>>>
>>>>>Both error403.jsp and error404.jsp pages correctly showing UTF-8 symbols, but error.jsp:
>>>>>
>>>>><%_at_page contentType="text/html"%>
>>>>><%_at_page pageEncoding="UTF-8"%>
>>>>><jsp:include page="jspf/header.jsp" />
>>>>><b>Ошибка:</b><br />
>>>>><%=((Throwable)request.getAttribute("javax.servlet.error.exception")).toString()%><br />
>>>>><button onclick="goback()">Назад</button>
>>>>><jsp:include page="jspf/footer.jsp" />
>>>>>
>>>>>
>>>>>
>>>>>Show '?' signes instead russian symbols.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>I've extended our unit test to also cover the scenario you've described,
>>>>and I'm unable
>>>>to reproduce the issue.
>>>>
>>>>What is the value of the Content-Type header of your response?
>>>>
>>>>Could it be that you have already written output to the response writer
>>>>*before*
>>>>the Throwable is raised and the request is forwarded to "/error.jsp"?
>>>>
>>>>If so, it will be impossible for "/error.jsp" to change the response
>>>>encoding,
>>>>which is why you are seeing the '?' in the response.
>>>>
>>>>An error page is able to set the response encoding only if no output has
>>>>been
>>>>written to the response before the error page is invoked.
>>>>
>>>>Hope this helps.
>>>>
>>>>
>>>>
>>>>
>>>Thank you, Jan.
>>>
>>>My servlet the Throwable is raised calls this code
>>>
>>>response.setContentType("text/html;charset=UTF-8");
>>>response.setCharacterEncoding("UTF-8");
>>>
>>>at the begin of 'processRequest' method, BEFORE ServletException throws.
>>>
>>>After i have refactored my servlet-code UTF-symbols o error.jsp began to be displayed correctly.
>>>
>>>
>>>WBR, Dmitry
>>>
>>>
>>>
>>>
>>Glad this is working!
>>
>>But the above calls in your servlet should not have prevented this from
>>working in the first place, unless your servlet already did output data
>>to the response before throwing the exception.
>>
>>Was that the case, or can you describe how you've refactored your
>>servlet in order to make this work?
>>
>>Thanks,
>>
>>
>>Jan
>>
>>
>>
>
>My old servlet has:
>
>...
>
> try {
>
> response.setContentType("text/html;charset=UTF-8");
> response.setCharacterEncoding("UTF-8");
> PrintWriter out = response.getWriter();
> ...
> out.print(functionThatCanThrowsException());
> ...
>
> } catch (MyOwnException e) {
>
> throw new ServletException(e.toString());
>
> }
>
>...
>
>And i refactor it into:
>
>...
>
> String body = "";
>
> try {
>
> body = functionThatCanThrowsException();
>
> } catch (MyOwnException e) {
>
> throw new ServletException(e.toString());
>
> }
>
> response.setContentType("text/html;charset=UTF-8");
> response.setCharacterEncoding("UTF-8");
> PrintWriter out = response.getWriter();
> ...
> out.print(body);
>
>

OK, this makes sense!

Notice that when you acquire the response writer, the response encoding
is set in stone and may no longer be changed (e.g., by the error page).

By delaying acquisition of the response writer until after the code that
may throw the exception has executed, you give the error page an opportunity
to change the response encoding in case an exception is thrown.

Thanks,


Jan


> ...
>
>...
>
>
>
>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>>For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>>
>>>
>>>
>>>
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>
>>
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>
>
>