File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to tell browsers which encoding to use from inside the servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to tell browsers which encoding to use from inside the servlet?" Watch "How to tell browsers which encoding to use from inside the servlet?" New topic
Author

How to tell browsers which encoding to use from inside the servlet?

Alexander Pantaleev
Greenhorn

Joined: Jun 14, 2002
Posts: 5
Here's my code:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (filterConfig == null) return;
HttpSession session = ((HttpServletRequest)request).getSession();
if (session.getAttribute("locale") == null) session.setAttribute("locale", request.getLocale());
response.setLocale((Locale)session.getAttribute("locale"));
chain.doFilter(request, response);

}
This should set the character encoding of the response to that of the request (I'm using cyrillic strings in the output jsp page). For some reason, however, it does not work. It does not work, either, when I try to set the locale explicitly (like response.setLocale(new Locale("BG", "")); ) I have tried using a standing stream, too (in order to call response.getWriter() in the filter and use the obtained PrintWriter after chain.doFilter. Guessed already? :-) Right, it does not work (but produces a different non-working result - the should-be cyrillic texts now consist entirely of ?'s).
Actually, the only thing I need is a way to tell the target browser which encoding to use, like the <%@ page contentType=" " %> directive. Is there a way to do that from inside the filter/servlet, based on a Locale object?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
can't u do that with using,
response.setHeader() method? also try to use Meta tag of HTML and have look at
this ..might b helpful...
regards
maulin
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
Have you tried using response.setContentType("text/html; charset=UTF-8") ?
This tells the browser what encoding the response is in via the charset directive.
I guess response.setLocale() should also set the appropriate encoding, but I'm less confident of it as compared to setContentType().
Alexander Pantaleev
Greenhorn

Joined: Jun 14, 2002
Posts: 5
None of the response.set... methods work.
I currently write <%@ page contentType="text/html; charset=UTF-8" %> on the top of every page, so I don't urgently need an answer to the question I posted, but I spent some time researching it with no success, so I thought somebody here might know.
After all, a single line of code in an already existing filter looks much finer than a single line of code in EVERY jsp page :-)
I guess the problem boils down to the servlet container I am using - Tomcat 4.0. Catalina is still under development, after all.
 
Consider Paul's rocket mass heater.
 
subject: How to tell browsers which encoding to use from inside the servlet?