HttpServletRequest和ServletRequest的区别

    xiaoxiao2021-03-26  28

    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); }

    转载请注明原文地址: https://ju.6miu.com/read-663623.html

    最新回复(0)