Servlet是什么
Servlet是JavaEE规范之一。规范就是接口
Servlet是Java Web三大组件之一,分别是Servlet程序 Filter过滤器 Listener监听器
Servlet就是运行在服务器上的一个Java小程序,它可以接受客户端发送过来的请求,并响应数据给客户端。
URL地址到Servlet程序的访问
Servlet的生命周期
1 执行Servlet构造器方法 第一次访问时创建Servlet程序时调用
2 执行init初始化方法 第一次访问时创建Servlet程序时调用
3 执行service方法 每次访问调用
4 执行destory销毁方法 web工程停止的时候调用
Servlet程序的实现
1.实现Servlet接口方式实现
1 编写一个类,实现Servlet接口
2 实现Service方法,处理请求(get或post类型的请求),并响应数据
3 到web.xml中去配置servlet程序的访问地址
Servlet程序
Public class HelloServlet implements Servlet{
@override
public void service(ServletRequest servletRequest,ServletResponse servletResponse) throws ServletException,IOException{
System.out.println("3 Service===Hello Servlet被访问了");
//类型转换(因为它有getMethod()方法)
HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
//获取请求的方式
String method=httpServletRequest.getMethod();
if("GET".equals(method)){
doGet();
}else if("POST".equals(method)){
doPost();
}
}
//做get请求的操作
public void doGet(){
System.out.println("get请求");
}
//做post请求的操作
public void doPost(){
System.out.println("post请求");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<!-- servlet 标签给 Tomcat 配置 Servlet 程序 -->
<servlet>
<!--servlet-name 标签 Servlet 程序起一个别名(一般是类名) -->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class 是 Servlet 程序的全类名-->
<servlet-class>com.atguigu.servlet.HelloServlet</servlet-class>
</servlet>
<!--servlet-mapping 标签给 servlet 程序配置访问地址-->
<servlet-mapping>
<!--servlet-name 标签的作用是告诉服务器,我当前配置的地址给哪个 Servlet 程序使用-->
<servlet-name>HelloServlet</servlet-name>
<!--url-pattern 标签配置访问地址 <br/>
/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径 <br/>
/hello 表示地址为:http://ip:port/工程路径/hello <br/>
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
2. 继承HttpServlet类方式实现
实际开发中,一般都是继承HttpServlet类而不是实现Servlet接口的方式去实现Servlet程序
1 编写一个类去继承HttpServlet类
2 根据业务需求写doGet和doPost方法
3 到web.xml中配置Servlet程序的访问地址
Servlet程序
public class HelloServlet2 extends HttpServlet {
//get
@override
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
System.out.println("HelloServlet2的doGet方法")
}
//post
@override
protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
System.out.println("HelloServlet2的doPost方法")
}
}
web.xml
<servlet>
<servlet-name>HelloServlet2</servlet-name>
<servlet-class>com.atguigu.servlet.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet2</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
Servlet相关类的继承关系
ServletConfig类
Servlet程序的配置类
Servlet程序和ServletConfig对象都是由Tomcat负责创建,我们负责使用
Servlet程序是第一次访问时创建,ServletConfig时每个Servlet程序创建时也会创建一个对应的ServletCofig对象
ServletConfig类3大作用:
1 获取Servlet程序的别名servlet-name的值
2 获取初始化参数init-param
3 获取ServletContext对象
web.xml
<!-- servlet 标签给 Tomcat 配置 Servlet 程序 -->
<servlet>
<!--servlet-name 标签 Servlet 程序起一个别名(一般是类名) -->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class 是 Servlet 程序的全类名-->
<servlet-class>com.atguigu.servlet.HelloServlet</servlet-class>
<!--init-param 是初始化参数-->
<init-param>
<!--是参数名-->
<param-name>username</param-name>
<!--是参数值-->
<param-value>root</param-value>
</init-param>
<!--init-param 是初始化参数-->
<init-param>
<!--是参数名-->
<param-name>url</param-name>
<!--是参数值-->
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</init-param>
</servlet>
<!--servlet-mapping 标签给 servlet 程序配置访问地址-->
<servlet-mapping>
<!--servlet-name 标签的作用是告诉服务器,我当前配置的地址给哪个 Servlet 程序使用-->
<servlet-name>HelloServlet</servlet-name>
<!--
url-pattern 标签配置访问地址 <br/>
/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径 <br/>
/hello 表示地址为:http://ip:port/工程路径/hello <br/>
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
servlet程序
@Override
public void init(ServletConfig servletConfig) throws ServletException{
super.init(sconfig);
System.out.println("2 init初始化方法")
//1、获取Servlet程序别名servlet-name的值
servletConfig.getServletName();
//2、获取初始化参数init-param
servletConfig.getInitParameter("username");
servletConfig.getInitParameter("url");
//3、获取ServletContext对象
servletConfig.getServletContext();
}
注意【【【】】】
重写init方法里面一定要调用父类的init(ServletConfig)操作
@Override
public void init(ServletConfig sc) throws ServletExecption{
super.init(sc);
}
ServletContext类
ServletContext类是什么
1 ServletContext是一个接口,表示Servlet上下文对象
2 一个web工程之有一个ServletContext对象实例
3 ServletContext对象是一个域对象
4 ServletContext实在web工程部署启动的时候创建。在web工程停止的时候销毁
域对象:可以像Map一样存取数据的对象,这里的域指的是存取数据的操作范围:整个web工程
存数据 取数据 删除数据 Map put() get() remove() 域对象 setAttribute() getAttribute() removeAttribute()
ServletContext类的4个作用
1 获取web.xml中配置的上下文参数context-param
2 获取当前工程的路径,格式:/工程路径
3 获取工程部署后在服务器硬盘上的绝对路径
4 像Map一样存取数据
Servlet程序
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//获取ServletContext上下文对象
ServletContext context=getServletConfig().getServletContext;
//1 获取web.xml中配置的上下文参数context-param
context.getInitParameter("username");
//2 获取当前工程路径,格式: /工程路径
context.getContextPath();
//3 获取工程部署后在服务器硬盘上的绝对路径
// '/'斜杠被服务器解析地址为http://ip:port/工程名/ 映射到IDEA代码的web目录
context.getRealPath("/"); //工程部署路径
context.getRealPath("/css") //工程下css目录的绝对路径
context.getRealPath("/imgs/1.jpg") //工程下imgs目录1.jpg文件的绝对路径
}
web.xml
<!--context-param 是上下文参数(它属于整个 web 工程)-->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<!--context-param 是上下文参数(它属于整个 web 工程)-->
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>
ServletContext像Map一样存取数据
ContextServler1代码
public class ContextServlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取 ServletContext 对象
ServletContext context = getServletContext();
System.out.println(context);
System.out.println("保存之前: Context1 获取 key1 的值是:"+ context.getAttribute("key1"));
context.setAttribute("key1", "value1");
System.out.println("Context1 中获取域数据 key1 的值是:"+ context.getAttribute("key1"));
}
}
ContextServlet2代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println(context);
System.out.println("Context2 中获取域数据 key1 的值是:"+ context.getAttribute("key1"));
}