目录
什么是Servlet
- Servlet是JavaEE规范之一,规范就是接口

- Servlet是JavaWeb三大组件之一,三大组件分别是:Servlet程序、Filter过滤器、Listener监听器
- Servlet是运行在服务器上的一个java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端
手动实现Servlet程序
- 编写一个类去实现Servlet接口
- 实现service方法,处理请求,并响应数据
- 到web.xml中去配置servlet程序的访问地址
创建项目:

创建类HelloServlet:
java
package com.qcby.servlet0724;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* service方法是专门用来处理请求和响应的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("HelloServlet被访问了");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}

web.xml:
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.qcby.servlet0724.HelloServlet</servlet-class>
</servlet>
<!--servlet-mapping标签:给servlet程序配置访问地址-->
<servlet-mapping>
<!--servlet-name标签:告诉服务器,我当前配置的地址给哪个servlet程序使用-->
<servlet-name>HelloServlet</servlet-name>
<!--url-pattern标签:配置访问地址
/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径
/hello 表示地址为:http://ip:port/工程路径/hello
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
项目结构:

启动:

访问/hello时:


注意:
- 两个servlet-name标签内容要相同
- url-pattern中要加/
url地址如何定位到Servlet程序去访问

Servlet的生命周期
- 执行Servlet
- 执行init初始化方法
- 执行service方法
- 执行destroy销毁方法
第一、二步是在第一次访问时创建servlet程序会调用
第三步每次访问都会调用
第四步在web工程停止时调用
java
package com.qcby.servlet0724;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
public HelloServlet() {
System.out.println("1 构造器方法");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2 init初始化");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* service方法是专门用来处理请求和响应的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3 service---HelloServlet被访问了");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("4 destroy销毁方法");
}
}


GET和POST请求的分发处理
在webapp下创建a.html:
get请求
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet0724/hello" method="get">
<input type="submit">
</form>
</body>
</html>
启动:

点击提交:
post请求
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet0724/hello" method="post">
<input type="submit">
</form>
</body>
</html>
启动:

添加获取请求:

完善:


完善:

通过继承HttpServlet类实现Servlet程序
一般在实际项目开发中,都是使用继承HttpServlet类的方式去实现Servlet程序
- 编写一个类去继承HttpServlet类
- 根据业务需要重写doGet或doPost方法
- 到web.xml中配置Servlet程序的访问地址
创建HelloServlet2类:
java
package com.qcby.servlet0724;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet2 extends HttpServlet {
/**
* 在get请求时调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("HelloServlet2的doGet方法");
}
/**
* 在post请求时调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("HelloServlet2的doPost方法");
}
}
web.xml:

a.html:
启动:


IDEA菜单生成Servlet程序

(取消勾选注解)


启动:

Servlet类的继承体系

ServletConfig类
ServletConfig类从类名上来看,就知道是Servlet程序的配置信息类
Servlet程序和ServletConfig对象都是由tomcat负责创建,我们负责使用
Servlet程序默认是第一次访问时创建,ServletConfig是每个Servlet程序创建时就创建一个对应的ServletConfig对象
每个ServletConfig对象对应自己的Servlet程序,互不影响
作用:
-
可以获取Servlet程序的别名servlet-name的值
-
获取初始化参数init-param
-
获取ServletContext对象

访问输出为:

启动输出为:

注意:
重写init方法一定要调用父类的super.init(config)

ServletContext类
什么是ServletContext类
- ServletContext是一个接口,它表示Servlet上下文对象
- 一个web工程只有一个ServletContext对象实例
- ServletContext对象是一个域对象(域对象是可以像Map一样存取数据的对象,这里的域指的是存取数据的操作范围,也就是整个web工程)
- ServletContext是在web工程部署启动时创建,在web工程停止时销毁
| | 存数据 | 取数据 | 删除数据 |
| Map | put() | get() | remove() |
域对象 | setAttribute() | getAttribute() | removeAttribute() |
---|
ServletContext类的四个作用
-
获取web.xml中配置的上下文参数context-param
-
获取当前的工程路径,格式:/工程路径
-
获取工程部署后在服务器硬盘上的绝对路径
-
像Map一样存取数据
创建ContextServlet类
配置:



启动输出为:


启动输出为:


启动输出为:

1-3完整代码:
java
package com.qcby.servlet0724;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class ContextServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取web.xml中配置的上下文参数context-param
ServletContext context = getServletConfig().getServletContext();
String username = context.getInitParameter("username");
System.out.println("context-param参数username的值是:"+username);
String password = context.getInitParameter("password");
System.out.println("context-param参数password的值是:"+password);
//获取当前的工程路径,格式:/工程路径
System.out.println("当前工程路径为:"+context.getContextPath());
//获取工程部署后在服务器硬盘上的绝对路径
/**
* / 斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到IDEA代码的webapp目录
*/
System.out.println("工程部署的路径是:"+context.getRealPath("/"));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
创建ContextServlet1
配置:


启动输出为:


