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 ...
随机推荐
- ThreadLocal源码阅读
package java.lang; import java.lang.ref.WeakReference; import java.util.Objects; import java.util.co ...
- jsp 知识点总结
JSP 入门案例(计算器的实现) 对于jsp 学习的一些总结 1.使用Dreamweaver 搭建网页 计算器 第一个数字 符号 + - * / 第二个数字 <form id="for ...
- DNS服务器红帽5.4搭建图文教程!!!
DNS服务器 挂载光盘 mount 查看光盘所在位置 mount -t iso9660 设备目录 /mnt 表示挂载 软件包安装 所有软件包都在Server目录下 rpm -ivh /mnt/Serv ...
- SpringBootTest 测试工具
以下内容,翻译自官方文档,并结合了学习过程的demo. Spring Boot提供了许多实用程序和注解,帮助测试应用程序.测试支持由两个模块提供:spring-boot-test 包含核心项,spri ...
- Python基础复习函数篇
目录 1.猴子补丁2. global和nonlocal关键字3.迭代器和生成器4.递归函数5.高阶函数和lamdba函数6.闭包7.装饰器 1. 猴子补丁 猴子补丁主要用于在不修改已有代码情况下修 ...
- 数据库并发处理 - 上的一把好"锁"
为什么要有锁? 我们都是知道,数据库中锁的设计是解决多用户同时访问共享资源时的并发问题.在访问共享资源时,锁定义了用户访问的规则.根据加锁的范围,MySQL 中的锁可大致分成全局锁,表级锁和行锁三类. ...
- zTree 节点勾选取消勾选 选中取消选中
zTreeObj.cancelSelectedNode function 举例 取消当前所有被选中节点的选中状态 var treeObj = $.fn.zTree.getZTreeObj(" ...
- Java爬虫一键爬取结果并保存为Excel
Java爬虫一键爬取结果并保存为Excel 将爬取结果保存为一个Excel表格 官方没有给出导出Excel 的教程 这里我就发一个导出为Excel的教程 导包 因为个人爱好 我喜欢用Gradle所以这 ...
- php配置xdebug插件,断点调试
xdebug 下载地址:https://xdebug.org 1.项目目录下新建phpinfo(); 文件: 2.快速查找符合自己的phpxdebug插件: https://xdebug.org/wi ...
- C++Primer第五版 6.1节练习
练习6.1:实参和形参的区别是什么? 通俗解释: 实参是形参的初始值.编译器能以任意可行的顺序对实参求值.实参的类型必须与对应的形参类型匹配. 详解1) 形参变量只有在函数被调用时才会分配内存,调用结 ...