01: package vqwiki.servlets;
02:
03: import java.io.IOException;
04: import javax.servlet.ServletException;
05: import javax.servlet.ServletRequest;
06: import javax.servlet.ServletResponse;
07: import javax.servlet.http.HttpServletRequest;
08: import javax.servlet.http.HttpSession;
09: import javax.servlet.Filter;
10: import javax.servlet.FilterConfig;
11: import javax.servlet.FilterChain;
12:
13: /**
14: * Sets request encoding for servlets to encoding used by JSTL formating tags.
15: * JSTL formating tags used for displaying localized messages
16: * are setting session-scoped variable
17: * <code>javax.servlet.jsp.jstl.fmt.request.charset</code>.
18: * This filter uses the variable to set request encoding
19: * for servlets, as they cannot use <fmt:requestEncoding/>
20: * as JSP pages.
21: *
22: * @author Martin Kuba makub@ics.muni.cz
23: */
24: public final class SetCharacterEncodingFilter implements Filter {
25:
26: /**
27: *
28: */
29: public void init(FilterConfig filterConfig) throws ServletException {
30: }
31:
32: /**
33: *
34: */
35: public void destroy() {
36: }
37:
38: /**
39: * Sets request encoding.
40: */
41: public void doFilter(ServletRequest request,
42: ServletResponse response, FilterChain chain)
43: throws IOException, ServletException {
44: if (request.getCharacterEncoding() == null) {
45: //set response locale according to request locale
46: response.setLocale(request.getLocale());
47: //if fmt tags specified output encoding already, use it
48: if (request instanceof HttpServletRequest) {
49: HttpSession ses = ((HttpServletRequest) request)
50: .getSession(false);
51: if (ses != null) {
52: String encoding = (String) ses
53: .getAttribute("javax.servlet.jsp.jstl.fmt.request.charset");
54: if (encoding != null) {
55: request.setCharacterEncoding(encoding);
56: }
57: }
58: }
59: }
60: chain.doFilter(request, response);
61: }
62: }
|