j2ee之Filter使用实例(页面跳转)
2015-09-07 21:19
260 查看
javax.servlet.Filter类中主要有三个方法。
先建立一个web工程,建立两个JSP页面,而本文中的程序主要实现的就死利用doFilter()方法,从index1.jsp跳转到index2.jsp。
建立好index1.jsp页面和index2.jsp。
下面配置一下WEB.xml,配置WEB.xml中的Filter和配置Servlet一样,类名和类包,然后是映射,很简单。
然后是index1.jsp页面,只需要写一个
测试一下是否跳转成功即可,index2.jsp内容随便(this is my page!)。
接下来是配置com包中Filter类中的doFilter()方法。具体代码如下:
以上。
public void destroy(); //销毁对象 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); //执行Filter响应的代码写在这个方法里 public void init(FilterConfig fConfig); //初始化对象
先建立一个web工程,建立两个JSP页面,而本文中的程序主要实现的就死利用doFilter()方法,从index1.jsp跳转到index2.jsp。
建立好index1.jsp页面和index2.jsp。
下面配置一下WEB.xml,配置WEB.xml中的Filter和配置Servlet一样,类名和类包,然后是映射,很简单。
<filter > <filter-name>filter</filter-name> <filter-class>com.Filter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> //应该与上面的filter-name一致 <url-pattern>*.action</url-pattern> //任何以.action结尾页面请求都可以被返回给filter </filter-mapping>
然后是index1.jsp页面,只需要写一个
<a href = "forward.jsp">点击此跳转致index2.jsp</a>
测试一下是否跳转成功即可,index2.jsp内容随便(this is my page!)。
接下来是配置com包中Filter类中的doFilter()方法。具体代码如下:
HttpServletRequest req = (HttpServletRequest) request; String path = req.getServletPath(); //此方法只有HttpServletRequest类中有,获得页面响应的路径 System.out.println(path); if("/forward.action".equals(path)){ //如果与index1.jsp中href中的地址一致则跳转index2.jsp request.getRequestDispatcher("index2.jsp").forward(request,response); }else{ //如果不一致则跳转index3.jsp页面 request.getRequestDispatcher("index3.jsp").forward(request,response); } chain.doFilter(request, response);
以上。
相关文章推荐
- java11
- C语言篇——小谈预处理命令
- wxpython初学者(三)
- 黑马程序员-OC基础之NSValue的使用
- 有关freemarker的语法,循环,逻辑
- Fragment
- C语言利用栈实现将中缀表达式转换为后缀表达式(即逆波兰式)
- LeetCode 89: Gray Code 格雷码
- get和post区别
- EasyUI -datagrid分页后台查询
- Http请求超时的一种处理方法
- 政策利好助大数据行业高增长 三领域概念股值得期待
- Java_jdbc 基础笔记之五 数据库连接 (ResultSet)
- web前端工程师
- 关于淘宝橱窗推荐-->用户行为追踪--http协议深入
- hdu 2084 数塔(动态规划)
- 测试使用的apache配置服务器
- DevExpress控件使用之多重坐标图形的绘制 z
- 🍀
- jQuery实现文本域提示文字点击后消失