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 ...
随机推荐
- idea编辑器的使用
编辑器下载和安装就不说了,网上每次版本都更换得好快 ,发新版的人很多idea2019:https://pan.baidu.com/s/1zc1wkQLLVxbXSjy4ISN4aQ 提取码:cgah, ...
- Synchronized解析——如果你愿意一层一层剥开我的心
前言 synchronized,是解决并发情况下数据同步访问问题的一把利刃.那么synchronized的底层原理是什么呢?下面我们来一层一层剥开它的心,就像剥洋葱一样,看个究竟. Synchroni ...
- 反射 API基本代码测试
ReflectBean.java package com.xiaojian.basics.reflect; /** * 使用反射的测试类 */ public class ReflectBean { / ...
- .Net 面试题整理(一)
1.C# 的三大特性? 封装.继承.多态 2.简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在类的内部才可以访问. ...
- XSS基础学习
XSS基础学习 By:Mirror王宇阳 什么是XSS XSS攻击是指在网页中嵌入一段恶意的客户端Js脚本代码片段,JS脚本恶意代码可以获取用户的Cookie.URL跳转.内容篡改.会话劫持--等. ...
- stars-one原创工具——m3u8视频下载合并器(kotlin)
一款可以下载m3u8.解密ts文件及合并ts文件的视频下载工具 蓝奏云下载地址 github地址 软件对你有帮助的话,不妨赞赏一波!感谢! 程序说明 采用多线程下载,可有效的提高下载速度 内置解密程序 ...
- 关于爬虫的日常复习(6)—— pyquery库
- Djaingo 随机生成验证码(PIL)
基础: https://www.cnblogs.com/wupeiqi/articles/5812291.html 实例: https://www.cnblogs.com/6324TV/p/88112 ...
- JS ES6补充
补充点:1.let const 2.字符串模板 3.箭头函数 4.对象的单体模式 5.面向对象 一.定义变量 A.var 特点: 1.定义全局变量 2.可以重复定义 3.变量名提升 <!DOC ...
- 在eclipse中导入源码
因为初学java有一个源码项目想要导入,在网上找了很多方法试了都不行,后来发现其实是想多了,这里说一个很简洁的方法.* 1.首先点eclipse中的File然后点import, 2. 然后选Gener ...