过滤器
过滤器的作用是对url访问进行拦截,比如:有一个需要登陆的系统,登陆成功后便跳转到index.jsp。如果用户直接输入index.jsp。这时候后台要对这样的行为进行拦截。
1、过滤器需要实现servlet的Filter接口。并且实现doFilter方法。
2、过滤器要起作用还要配置过滤器和拦截的对象。在web.xml中配置。
一个简单的登陆过滤器的配置例子:
1 | <!--配置过滤器--> |
监听器
1、监听器一共有8种类型,主要是监听对象的创建和销毁,属性的增加和减少。其中有如下几种
监听对象的创建和销毁的监听器
(1)ServletContextlistener 监听ServletContext对象
(2)HttpSessionListener 监听HttpSession对象
(3)ServletRequestListener 监听ServletRequest对象
监听对象属性的增加、修改和删除的事件监听器
(4)ServletContextAttributeListener 监听ServletContext对象属性变更
(5)HttpSessionAttributeListener监听HttpSession对象属性变更
(6)ServletRequestAttributeListener监听Servlet对象的属性的变更
监听绑定到HttpSession域中的某个对象状态的事件监听器
(7)HttpSessionBindingListener 监听session的绑定和解除
(8)HttpSessionActivationListener 会话持久层。
2、一个统计网站当前在线人数的例子的监听器例子
1 | ** |
每当有一个新的连接的时候,会触发request的监听器。当有一个session创建的时候就会触发sessionCreated函数,将全局的变量加一。当一个session过期的时候就会触发sessionDestred函数,将全局变量减一。
jsp9大内置对象
一、Request 对象
客户端的请求信息封装在request对象中,是HttpServletRequest的实例
常用的方法:
1、Object getAttribute(String name)
返回指定属性的属性值
2、Enumeration getAttributeNames()
返回所有可用属性名的枚举
3、String getCharacterEncoding()
返回字符串编码格式
4、int getContentLength()
返回请求体的长度
5、String getContentType()
返回请求体的MIME的长度
6、ServletInputStream getInputStream()
得到请求体中一行的二进制流数据
7、String getParameter(String name)
返回name指定参数的参数值
8、Enumeration getParameterNames()
返回可用参数名的枚举
9、String[] getParameterValues(String name)
返回包含参数name的
10、String getProtocol()
返回请求用的协议类型及版本号
11、String getScheme()
返回请求的结构类型 http、https、ftp
12、String getServerName()
返回接收请求的服务器主机名
13、int getServerPort
返回服务器接收此请求的所用的端口号
14、BufferReader getReader()
返回解码过了的请求体
15、String getRemoteAddr()
返回发送此请求的客户端IP地址
16、String getRemoteHost
返回发送此请求的客户端主机名
17、void setAttribute(String key,Object obj)
设置属性的属性值
18、String getRealPath(String path)
返回一虚拟路径的真实路径
二、Response 对象
常用方法:
1、String getCharacterEncoding()
返回响应用的是何种字符串编码格式
2、ServletOutputStream getOutputStream()
返回相应的一个二进制的输出流
3、PrintWriter getWriter()
返回可以向客户端输出字符的一个对象
4、void setContentLength(int len)
设置响应的长度
5、void setContentType(String type)
设置响应的MIME类型
6、void sendRedirect(String location)
重新定向客户端的请求
三、session 对象
从客户端连到服务器的一个WebApplication开始。直到客户端与服务器断开连接为止。是HttpSession 的实例
常用方法:
1、long getCreationTime()
返回Session的创建时间
2、String getId()
返回唯一的ID号
3、long getLastAccessedTime()
返回此客户端最近一次请求时间
4、int getMaxInactiveInterval()
获得两个请求间隔多长时间此SESSION被取消
5、void setMaxInactiveInterval(int ms)
设置两次请求间隔时长。被销毁
6、String[] getValueNames()
返回一个包含此SESSION中所有可用属性的数组
7、void invalidate()
使该session不可用
8、boolean isNew()
是不是新的,客户端新加入
9、void removeValue(String name)
删除SESSION中指定属性
四、out 对象
out对象是JspWriter类的实例,是向客户端输出内容常用的对象
常用方法:
1、void clear()
清除缓冲区的内容
2、void clearBuffer()
清除缓冲区当前内容
3、void flush()
清空流
4、int getBufferSize()
返回缓冲区的字节大小
5、int getRemaining()
返回缓冲区还剩余多少可用
6、boolean isAutoFlush()
返回缓冲区满时时自动清空还是抛出异常
7、void close()
关闭输出流
四、page对象
page对象指向当前JSP页面本身,
常用方法:
1、class getClass
返回此Object的类
2、int hashCode()
3、boolean equals(Object obj)
4、void copy(Object objet)
5、Object clone()
6、String toString()
7、void notify()
唤醒一个等待的线程
8、void notifyAll()
唤醒所有等待的线程
9、void wait(int timeout)
10、void wait()
11、void enterMonitor()
对Object对象加锁
12、void exitMonitor()
对Object对象开锁
五、application 对象
实现了用户间数据共享,可存放全局变量。它开始于服务器的启动,直到服务器关闭。是ServletContext类的实例
常用方法:
1、Object getAttribute(String name)
2、void setAttribute(String name,Object obj)
3、void removeAttribute(String name)
删除一属性及其属性值
4、Enumeration getAttributeNames()
返回所有可用的属性值
5、String getServerInfo()
返回jsp引擎的名称和版本号
6、String getRealPath(String path)
返回一虚拟路径的真实路径
7、ServletContext getContext(String uripath)
返回指定WebApplication的application对象
8、int getMajorVersion()
返回服务器支持的Servlet API 的最大版本号
9、int getMinorVerion()
支持最小版本号
10、String getMineType(String file)
返回指定文件的MINE类型
11、URL getResource(String path)
返回指定资源的URL路径
12、InputStream getResourceAsStream(String uripath)
返回指定资源的输入流
13、RequestDispacher getRequestDispatcher(String uripath)
返回指定资源的RequestDispachcer对象
14、Servlet getServlet(String name)
返回指定名的Servlet
15、Enumeration getServlets()
返回所有的Servlet的枚举
16、Enumeration getServletNames()
返回所有的Servlet名的枚举
17、void log(Stirng msg)
写入日志文件中
六、exception 对象
1、String getMessage()
返回异常的描述消息
2、String toString()
返回异常的简短描述消息
3、void printStackTrace()
显示异常及栈轨道
4、Throwable FillInStackTrace()
重写异常执行栈轨迹
七、pageContext对象
页面中所有功能的集大成者
1、JspWriter getOut()
返回当前客户端响应被使用的JspWriter流(out)
2、HttpSession getSession()
返回当前页中的HttpSession对象
3、Object getPage()
4、ServletRequest getRequest()
返回当前页的request对象
5、ServletResponse getResponse()
返回当前页的response对象
6、Exception getException()
7、ServletConfig getServletConfig()
8、ServletContext getServletContext()
9、void setAttribute(String name,Object obj)
10、Object getAttribute(String name)
11、Objcet findAttribute(String name)
12、void removeAttribute(String name)
13、void removeAttribute(String name,int scope)
14、int getAttributeScope(String name)
15、Enumeration getAttributeNamesInScope(int scope)
16、void release()
释放pageContext所占用的资源
17、void forward(String relativeUrlPath)
重定向到另一个页面
18、void include(String relativeUrlPath)
包含另一个文件
八、config对象
相关的配置信息
1、ServletContext getServletContext()
返回服务器相关信息的ServletContext对象
2、String getInitParameter(String name)
初始参数
3、Enumeration getInitParameterNames()
返回Servlet初始化所需所有参数的枚举