views:

170

answers:

1

Hi folks,

I have some Servlet that explicity sets the character encoding and redirect to some servlet

class Servlet1 extends HttpServle{
   void doGet(..... ){
        // ...
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8"):
        //......
        response.redirect(servlet2);
    }
}

class Servlet2 extends HttpServle{
   void doGet(..... ){
        // ...
        request.getCharacterEncoding();  // prints null ?? why???
        //......

    }
}

So, why the character encoding not being send with the request?

+2  A: 

The HttpServletResponse#setCharacterEncoding() sets the encoding on the current response, not on the subsequent request. It's also not the client's responsibility to pass it back on the subsequent request. What you're trying to achieve is simply not possible without interaction of the client, which it is not required to do in this case. To get what you want, the client has to set the HTTP Content-Type header with a charset attribute itself. Check it with a HTTP header debugger tool like Firebug and you'll see that it is absent in the request.

BalusC
Thanks BalusC, you actually forwarded me to this good tool (FireBug) and it was actually helpful.So, How I can tell the client (from the Servlet code) to set the charset before redirect the response back to the new URL?as you know, dumped http-equiv don't do that http://stackoverflow.com/questions/2861886/does-a-servlet-knows-the-encoding-of-the-sent-form-that-specified-using-http-equi/2862066#2862066
Mohammed
You can't. I would otherwise have answered that here and in the linked question. Just set the response content type. The client will use it. I however put question marks on using GB2312, I would personally prefer UTF-8 over this since this is more widely supported and has a better character coverage. But of course this depends after all on your audience. If you're targeting on GB2312 folks only, then it should work without problems.
BalusC
About GB2312, It is just user requirements !!
Mohammed
Ah OK. I'll keep this in mind and refrain me from commenting about this on your future questions involving GB2312 ;)
BalusC