内容简介:最近没事儿瞎折腾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 空白页的问题》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- HTTP status 302 引发的 CURL response 空白问题思考
- Unicode空白是否有Python常量?
- EmptyPage(空白页组件)原理与使用
- ios – UITableView在顶部获得了空白[封闭]
- RecyclerView瀑布流空白、重新排序原因及解决办法
- 利用空白gif图片,实现数据上报,数据埋点
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
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》 这本书的介绍吧!