idea jsp html 空白页的问题

栏目: JSP · 发布时间: 5年前

内容简介:最近没事儿瞎折腾java web,在idea中,突然发现无法显示页面了。为什么会出现这个问题?接触了过滤器的内容,然后在项目中添加了这样的一个过滤器,用来对post过来的数据进行ut8编码。

摘要

最近没事儿瞎折腾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);
    }

结语

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


以上所述就是小编给大家介绍的《idea jsp html 空白页的问题》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Visual Thinking

Visual Thinking

Colin Ware / Morgan Kaufmann / 2008-4-18 / USD 49.95

Increasingly, designers need to present information in ways that aid their audiences thinking process. Fortunately, results from the relatively new science of human visual perception provide valuable ......一起来看看 《Visual Thinking》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具