Unable to access session data in servlet filter on app engine dev server
I was trying to write a session filter to centralize authorization check in my app engine web app.
My filter looks like this :
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpResp = (HttpServletResponse) request;
HttpSession session = httpReq.getSession();
httpReq.setCharacterEncoding("UTF-8");
httpResp.setCharacterEncoding("UTF-8");
httpResp.setContentType("application/json");
Boolean isAuthorized = (Boolean) session.getAttribute("isAuthorized");
if (!i开发者_开发知识库sAuthorized || isAuthorized == null) {
httpResp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} else {
chain.doFilter(request, response);
}
}
But when I'm launching app engine dev server I'm greeted with this exception : "org.mortbay.jetty.request cannot be cast to javax.servlet.http.httpservletresponse". What am I doing wrong?
You are trying to cast "request" object as response.
change as follows
HttpServletResponse httpResp = (HttpServletResponse) response;
精彩评论