欢迎页面

设置欢迎页面后
http://localhost:8080/项目名/
与效果相同
http://localhost:8080/项目名/xx/index.jsp
跳转和包含
跳转:
1.sendRedirect
response.sendRedirect("/项目名/xx/xx.jsp");
2.forward
ServletContext application=this.getServletContext();
RequestDispatcher rd=application.getRequestDispatcher("/xx.jsp");
rd.forward(request,response);
包含:
ServletContext application=this.getServletContext();
RequestDispatcher rd=application.getRequestDispatcher("/xx.jsp");
rd.include(request,response);
初始化参数
全局参数
定义:

获取:
ServletContext application=this.getServletContext();
application.getInitparameter("参数名称");
局部参数
定义:

获取:
this.getInitparameter("参数名称");
MVC模式
Model(模型):JavaBean
View(视图):JSP
Controller(控制):Servlet
异常集中处理
略
过滤器
服务器端组件,可以截取用户端的请求与响应信息,并对这些信息过滤。

生命周期:
1、实例化:启动服务器时实例化加载过滤器
2、初始化:接着初始化,调用初始化方法init()
3、过滤:在每次过滤器获得请求时执行过滤方法doFilter()
4、销毁:服务器关闭调用destroy()
实现过滤器步骤:
1、创建过滤器类实现Filter接口,重写init()、doFilter()、destroy()方法

——init():服务器启动时调用此方法,可读取web.xml中的过滤器参数
——doFilter():每当用户请求访问与过滤器关联的URL时,都会调用此方法
——destroy():关闭服务器时会调用此方法
2、在web.xml中注册配置过滤器。

3、访问过滤器

过滤器链:
多个相等的URL会形成过滤器链


过滤器执行doFilter()方法时,先执行输出语句。

再通过如下图所示方法,向下传递到下一个过滤器:

过滤器的分类:
1、request:用户直接访问页面,调用此过滤器
2、forward:转发,调用此过滤器
request.getRequestDispatcher("页面.jsp").forward(request, response);//请求转发
3、include:包含,调用此过滤器
request.getRequestDispatcher("页面.jsp").include(request, response);//包含
4、error:错误,调用此过滤器
监听器
servlet规范中定义的一种特殊类
用于监听servletContext、HttpSession和ServletRequest等域对象的创建和销毁事件
按监听对象分类:
1、监听应用程序环境对象servletContext
2、监听用户会话对象HttpSession
3、监听请求消息对象ServletRequest
按监听事件分类:
1、域对象自身的创建和销毁
servletContext、HttpSession、ServletRequest三个对象



2、域对象中的属性增加和删除

3、绑定到HttpSession域中的某个对象的状态
略
加载顺序:监听器>过滤器>servlet
网友评论