HttpServlet接口介绍
从我的上一篇博文的模板制作看出,我们制作的模板中实现的接口是HttpServlet接口,而并非是Servlet接口。
这其实这是证实了我在WEB13:Servlet学习01中的第二句写到的:
你写的类必须实现Servlet接口或者你写的类的父类及其以上必须实现Servlet接口。
通过Servlet源码我们可以看到,源码地址>>>>提取码:x13t
其实HttpServlet的父类的父类实现了Servlet接口
而且在模板中也没有了service方法,其实这在源码中也可以看出来,service方法最后执行的是doGet方法
所以,我们直接使用了doGet方法了!
ServletContext
这个比较重要!!!!
ServletContext是什么?
ServletContext其实就是一个web应用的上下文对象,内部封装的web应用信息。
这一个项目中,我们可以出现多个Servlet,比如这里我们就有两个Servlet
而在一个项目中我们只有一个ServletContext对象
生命周期
创建:服务器启动或者项目发布
销毁:服务器关闭
如何获得ServletContext?
方法一:
使用SerevletConfig对象获得(不常用),有时我们没有这个ServletConfig对象不就完犊子了啊!
方法二:
使用this获得
这里通过查源码解释一下
这里我们实现的接口是HttpServlet接口!但是这里没有我们的getServletContext方法,我们去哪里找呢?父类!
没找到。去父类的父类
这里其实也是有一个ServletConfig的,因此我们不用管这个ServletConfig了。我们this就完事了!
ServetContext干什么?
1.获得初始化参数:
其实跟在WEB013:Servlet学习01中提到的init方法类似。
用法:因为ServletContext是一个全局的,所以我们配置的参数也应该是全局的,而不能放在某个Servlet之中。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>aa</param-name>
<param-value>aaaAAAAA</param-value>
</context-param>
<context-param>
<param-name>bb</param-name>
<param-value>bbbBBBB</param-value>
</context-param>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.yl.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
获取的方法doGet()
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = this.getServletContext();
String init = context.getInitParameter("aa");
System.out.println(init);
}
2.获得该web的所有资源的绝对路径:
我们通过测试获取不同位置的文件绝对路径来理解。现在我们获取1.txt/2.txt/3.txt/4.txt
代码如下
package com.yl.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = this.getServletContext();
// 获取在WEB-INF里的1.txt的绝对路径
String path1 = context.getRealPath("WEB-INF/1.txt");
//获取在根目录下的2.txt
String path2 = context.getRealPath("2.txt");
//获取src下的3.txt
String path3 = context.getRealPath("WEB-INF/classes/3.txt");
//获取src下的3.txt
String path4 = MyServlet.class.getClassLoader().getResource("3.txt").getPath();
// 是否可以获取WEB06下的4.txt
System.out.println(path1+"\n"+path2+"\n"+path3+"\n"+path4);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3.是一个域对象:
什么是域对象?
就是一个存储数据的地方。就类似我们Java集合中的Map集合,存的时候,把值放进域里,并依靠一个key来取。
这里我们需要两个Servlet
第一个Servlet负责存数据
第二个Servlet负责取数据
存数据
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = this.getServletContext();
context.setAttribute("name", "Tom");
}
取数据
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = this.getServletContext();
String name = (String)context.getAttribute("name");
System.out.println(name);
}