Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Alexander Pantaleev
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic