JSP&Servlet学习笔记----第5章
Servlet进阶API
- @Override
- public void init(ServletConfig config) throws ServletException {
- this.config = config;
- this.init();
- }
- public void init() throws ServletException {
- // NOOP by default
- }
ServletConfig相当于Servlet的设置信息代表对象。
- */
- @WebServlet(name = "FiveServlet",
- urlPatterns = {"/five.do"},
- initParams = {
- @WebInitParam(name = "name", value = "xiya"),
- @WebInitParam(name = "age", value = "24")
- })
- private String name;
- private String age;
- @Override
- public void init() throws ServletException {
- name = getInitParameter("name");
- age = getInitParameter("age");
- }
或者web.xml
- <servlet>
- <servlet-name>FiveServlet</servlet-name>
- <servlet-class>FiveServlet</servlet-class>
- <init-param>
- <param-name>name</param-name>
- <param-value>xiya</param-value>
- </init-param>
- <init-param>
- <param-name>age</param-name>
- <param-value>25</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>FiveServlet</servlet-name>
- <url-pattern>/five.do</url-pattern>
- </servlet-mapping>
web.xml中的设置会覆盖标注的设置。(标注的name属性需要和web.xml的<servlet-name>匹配)。
应用程序事件、监听器
HttpServletRequest事件、监听器
- public interface ServletRequestListener extends EventListener {
- /**
- * The request is about to go out of scope of the web application.
- * The default implementation is a NO-OP.
- * @param sre Information about the request
- */
- public default void requestDestroyed (ServletRequestEvent sre) {
- }
- /**
- * The request is about to come into scope of the web application.
- * The default implementation is a NO-OP.
- * @param sre Information about the request
- */
- public default void requestInitialized (ServletRequestEvent sre) {
- }
- }
测试代码:
- import javax.servlet.*;
- import javax.servlet.annotation.WebListener;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- /**
- * Created by N3verL4nd on 2017/3/1.
- */
- @WebListener
- @WebServlet(name = "ListenerServlet", urlPatterns = {"/listener.do"})
- public class ListenerServlet extends HttpServlet implements ServletRequestListener, ServletRequestAttributeListener{
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("doPost");
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("doGet");
- request.setAttribute("name", "xiya");
- request.setAttribute("name", "xiya1");
- }
- @Override
- public void requestDestroyed(ServletRequestEvent sre) {
- System.out.println("requestDestroyed");
- }
- @Override
- public void requestInitialized(ServletRequestEvent sre) {
- System.out.println("requestInitialized");
- }
- @Override
- public void attributeAdded(ServletRequestAttributeEvent srae) {
- System.out.println("attributeAdded");
- System.out.println("attributeAdded: " + srae.getName() + " " + srae.getValue());
- }
- @Override
- public void attributeRemoved(ServletRequestAttributeEvent srae) {
- System.out.println("attributeRemoved");
- System.out.println("attributeRemoved: " + srae.getName() + " " + srae.getValue());
- }
- @Override
- public void attributeReplaced(ServletRequestAttributeEvent srae) {
- System.out.println("attributeReplaced");
- System.out.println("attributeReplaced:" + srae.getName() + " " + srae.getValue());
- }
- }
访问:http://localhost:8080/jspRun/listener.do
attributeReplaced
attributeReplaced:org.apache.catalina.ASYNC_SUPPORTED true
doGet
attributeAdded
attributeAdded: name xiya
attributeReplaced
attributeReplaced:name xiya
requestDestroyed
HttpSession事件、监听器
- public interface HttpSessionListener extends EventListener {
- /**
- * Notification that a session was created.
- * The default implementation is a NO-OP.
- *
- * @param se
- * the notification event
- */
- public default void sessionCreated(HttpSessionEvent se) {
- }
- /**
- * Notification that a session is about to be invalidated.
- * The default implementation is a NO-OP.
- *
- * @param se
- * the notification event
- */
- public default void sessionDestroyed(HttpSessionEvent se) {
- }
- }
ServletContext事件、监听器
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebListener;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- /**
- * Created by N3verL4nd on 2017/3/1.
- */
- @WebListener
- @WebServlet(name = "ContextListenerServlet", urlPatterns = {"/contextListener.do"})
- public class ContextListenerServlet extends HttpServlet implements ServletContextListener{
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("doGet");
- }
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("contextInitialized");
- }
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- System.out.println("contextDestroyed");
- }
- }
[2017-03-01 04:02:31,096] Artifact jspRun:war exploded: Artifact is being deployed, please wait...
[2017-03-01 04:02:31,097] Artifact WeiBo:war exploded: Artifact is being deployed, please wait...
contextInitialized
[2017-03-01 04:02:31,448] Artifact jspRun:war exploded: Artifact is deployed successfully
[2017-03-01 04:02:31,450] Artifact jspRun:war exploded: Deploy took 354 milliseconds
由此可见,当整个Web应用程序加载Web容器之后,容器会生成一个ServletContext对象。
[2017-03-01 04:03:53,922] Artifact WeiBo:war exploded: Artifact is being deployed, please wait...
contextDestroyed
contextInitialized
[2017-03-01 04:03:54,580] Artifact jspRun:war exploded: Artifact is deployed successfully
[2017-03-01 04:03:54,580] Artifact jspRun:war exploded: Deploy took 659 milliseconds
[2017-03-01 04:03:55,270] Artifact WeiBo:war exploded: Artifact is deployed successfully
[2017-03-01 04:03:55,270] Artifact WeiBo:war exploded: Deploy took 1,348 milliseconds
过滤器
- public interface Filter {
- public default void init(FilterConfig filterConfig) throws ServletException {}
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException;
- public default void destroy() {}
- }
Filter接口的doFilter()方法类似于Servlet接口的service()方法。当请求来到容器,而容器发现调用Servlet的service()方法前,可以应用某过滤器,就会
JSP&Servlet学习笔记----第5章的更多相关文章
- JSP&Servlet学习笔记----第3章
Web容器是JSP/Servlet唯一认识的HTTP服务器. HTTP是基于请求/响应的无状态通信协议. 流程: 1.请求来到HTTP服务器 2.HTTP服务器将请求转交给Web容器 3.Web容器创 ...
- JSP&Servlet学习笔记----第6章
JSP与Servlet是一体两面的关系. JSP最终还是被编译为Servlet. <%@page contentType="text/html;charset=UTF-8" ...
- JSP&Servlet学习笔记----第4章
HTTP是基于请求/响应的无状态的通信协议. 使服务器记得此次请求与之后请求关系的方式,叫做会话管理. 隐藏域:由浏览器在每次请求时主动告知服务器多次请求间必要的信息.仅适用于一些简单的状态 管理,如 ...
- JSP&Servlet学习笔记----第1/2章
HTML(HyperText Markup Language):超文本标记语言 HTTP(HyperText Transfer Protocol):超文本传输协议 URL(Uniform Resour ...
- jsp&servlet学习笔记
1.路径引用问题 一个css.jsp.html.或者javascript文件从从一个工程复制到另一工程,如果引用的时候使用的时相对路径,看似没有错误,但是却一直引用不进来,这时候要使用绝对路径,这样才 ...
- 【JSP&Servlet学习笔记】5.Servlet进阶AIP、过滤器与监听器
Servlet接口上,与生命周期及请求服务相关的三个方法是init().service()与destory()方法.当Web容器加载Servlet类并实例化之后,会生成ServletConfig对象并 ...
- JSP Servlet学习笔记——使用fileupload上传文件
关键代码如下: index.jsp <body> <center> <h3>文件上传</h3> <font color="red&quo ...
- 【JSP&Servlet学习笔记】4.会话管理
Http本身是无状态通信协议,要进行会话管理的基本原理,就是将需要维护的状态回应给浏览器,由浏览器在下次请求时主动发送状态信息,让Web应用程序“得知”请求之间的关联. 隐藏字段是将状态信息以窗体中看 ...
- # jsp及servlet学习笔记
目录 jsp及servlet学习笔记 JSP(Java Server Page Java服务端网页) 指令和动作: servlet(小服务程序) jsp及servlet学习笔记 JSP(Java Se ...
随机推荐
- 「Luogu P1435」回文字串 解题报告
题面 主要大衣大意: 给定一个字符串,求至少加入多少个字符才能使字符串变成回文字符串 下面就是我一本正经的胡说八道题解 思路: 很显然,这应该是一道典型的最长公共子序列的题目 因此,主要思想就是DP ...
- Tomcat黑窗口中对于中文乱码问题的解决
存在的问题: 如标题,下图所示,启动tomcat时黑窗口中中文乱码,影响查看程序打印信息 解决方案: tomcat安装/解压目录中,conf 文件夹下 logging.properties 文件中,代 ...
- Dockerfile文件记录(用于后端项目部署)
Dockerfile文件记录(用于后端项目部署) 本教程依据个人理解并经过实际验证为正确,特此记录下来,权当笔记. 注:基于linux操作系统(敏感信息都进行了处理) 此文结合另一篇博客共同构成后端服 ...
- zTree 节点勾选取消勾选 选中取消选中
zTreeObj.cancelSelectedNode function 举例 取消当前所有被选中节点的选中状态 var treeObj = $.fn.zTree.getZTreeObj(" ...
- linux下安装cmake方法(1)---下载压缩包
OpenCV 2.2以后的版本需要使用Cmake生成makefile文件,因此需要先安装cmake:还有其它一些软件都需要先安装cmake 1.在linux环境下打开网页浏览器,输入网址:http:/ ...
- 【转】C#中base关键字的几种用法:base()
转:https://blog.csdn.net/cplvfx/article/details/82982862 base其实最大的使用地方在面相对象开发的多态性上,base可以完成创建派生类实例时调用 ...
- Java 从入门到进阶之路(二十二)
在之前的文章我们介绍了一下 Java 中的 集合框架中的Collection 中的一些常用方法,本章我们来看一下 Java 集合框架中的Collection 的迭代器 Iterator. 当我们创建 ...
- 浅析PHP类的自动加载和命名空间
php是使用require(require_once)和include(include_once)关键字加载类文件.但是在实际的开发工程中我们基本上不会去使用这些关键字去加载类. 因为这样做会使得代码 ...
- Oracle索引大全
文档结构如下: 前言: Oracle 官方文档对索引的描述真是弱透了,对索引的说明就是一坨……,support也没有很好的资料,下面还是用的官方上的内容经过自己的整理加上网上的资料. 索引类型: 索引 ...
- Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解
微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...