• idea jsp html 空白页的问题


    摘要

    最近没事儿瞎折腾java web,在idea中,突然发现无法显示页面了。

    问题

    为什么会出现这个问题?

    接触了过滤器的内容,然后在项目中添加了这样的一个过滤器,用来对post过来的数据进行ut8编码。

    package com.shop.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    /**
     * post请求过滤器
     */
    public class EncodingFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
    FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("UTF-8"); } @Override public void destroy() { } }

    注意,对过滤器的生命周期,是在服务器启动的时候,会对过滤器进行初始化,根据web.xml配置文件中的url-pattern进行过滤。

      <!--post请求过滤器配置-->
        <filter>
            <filter-name>post-encoding</filter-name>
            <filter-class>com.shop.filter.EncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>post-encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    这里进行的对所有的url进行过滤,在这种模式下,会对所有的请求,都会先进入doFilter方法,但是,在对请求进行处理之后,并没有放行,导致直接返回客户端了,所以页面一直是一个空白页。不管jsp还是html都是一样的。解决办法,在过滤器链中对其放行,让它继续执行之后的过滤器,如果没有则进行请求处理,然后相应,最后回到该过滤器,然后响应给客户端。

    所以解决办法如下:

     @Override
        public void doFilter(ServletRequest servletRequest, 
    ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("UTF-8"); //放行 filterChain.doFilter(servletRequest, servletResponse); }

    结语

    关于过滤器,一定要根据业务关系,要在满足一定条件的情况下,给与放行。

  • 相关阅读:
    【pycharm 密钥】pycharm 2017 密钥
    【jenkins 忘记密码】忘记Jenkins管理员密码的解决办法
    【git 报错】Could not read from remote repository.Please make sure you have the correct access rights.
    【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import
    【python接口自动化测试教程】00---00章节就代表开篇吧
    【python-strip】Python strip()方法
    认识map-reduce
    subprocess实用手册
    k8s学习路线
    nginx小知识
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/9072753.html
Copyright © 2020-2023  润新知