HttpServletRequest和ServletRequest都是接口HttpServletRequest继承自ServletRequest
HttpServletRequest比ServletRequest多了一些针对于Http协议的方法。 例如:getHeader(), getMethod() , getsession()
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { // 过滤用户请求,判断是否登录 HttpServletRequest httpServletRequest = (HttpServletRequest)request; HttpServletResponse httpServletResponse = (HttpServletResponse)response; httpServletResponse .setContentType("text/html;charset=utf-8"); httpServletRequest.setCharacterEncoding("utf-8"); httpServletResponse.setCharacterEncoding("utf-8"); String username = (String)httpServletRequest.getSession().getAttribute("username"); if (username == null) { String path = httpServletRequest.getContextPath(); httpServletResponse.sendRedirect(path+"/index.jsp"); } filterChain.doFilter(httpServletRequest, httpServletResponse); }
新闻热点
疑难解答