什么是servlet?
1.servlet是JavaEE规范之一。规范就是接口
2.servlet是javaWeb的三大组件之一,三大组件分别是servlet程序、Filter过滤器、Listener监听器。
3.servlet是运行在服务器上的java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端
手动实现Servlet程序
1、编写一个类去实现servlet接口
2、实现service方法,处理请求并响应数据
3、到web.xml中去配置servlet程序的访问地址
package com.xt.servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* ClassName: helloServlet
* Description:
* date: 2022/6/23 12:57
*
* @author tongy
* @since JDK 1.8
*/
public class helloServlet implements Servlet {
public helloServlet() {
System.out.println("1 构造器方法 ");
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("2 init方法");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
//service是专门为了处理请求和响应的
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
//强制类型转换(因为它有getMethod())
HttpServletRequest httpServletRequest= (HttpServletRequest) req;
//获取请求的方式
String method = httpServletRequest.getMethod();
if (method=="POST"){
doPost();
System.out.println("post方式");
}if (method=="GET"){
doGet();
System.out.println("GET方式");
}
}
//做get请求的操作
public void doGet(){
}
//做post请求的操作
public void doPost(){
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("4 destroy方法");
}
}
<?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-name是servlet程序的全类名-->
<servlet-class>com.xt.servlet.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>
<servlet>
<servlet-name>helloServlet2</servlet-name>
<servlet-class>com.xt.servlet.helloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet2</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
</web-app>
servlet过程

servlet的生命周期
1、执行servlet构造器方法
2、执行init初始化方法
第1、2步是在第一次访问的时候,创建servlet程序会调用
3、执行service方法
第3步,每次访问都会调用
4、执行destroy方法
第4步,在web工程停止的时候调用
通过继承HttpServlet实现Servlet程序
一般在实际项目开发中,都是使用继承HttpServlet的方式去实现Servlet程序。
1、编写一个类去继承HttpServlet
2、根据业务需要重写doGet()、doPost()
3、到web.xml中配置servlet程序的访问地址
package com.xt.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ClassName: helloServlet2
* Description:
* date: 2022/6/23 15:09
*
* @author tongy
* @since JDK 1.8
*/
public class helloServlet2 extends HttpServlet {
/**
* doGet() 在get请求的时候调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello servlet2的doGet()");
}
/**
* doPost() 在post请求的时候调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello servlet2的doPost()");
}
}
使用IDEA创建Servlet程序
New->Servlet

Servlet类的继承体系:

ServletConfig类
是Servlet程序的配置信息类
ServletConfig类的三大作用:
1、可以获取Servlet程序的别名,即:Servlet-name的值
2、获取初始化参数init-param
3、获取ServletContext对象
public void init(ServletConfig config) throws ServletException {
System.out.println("2 init方法");
// 1、可以获取Servlet程序的别名,即:Servlet-name的值
System.out.println("helloServlet的别名是:"+ config.getServletName());
// 2、获取初始化参数init-param
System.out.println("初始化参数username的值是"+config.getInitParameter("username"));
System.out.println("初始化参数url的值是"+config.getInitParameter("url"));
// 3、获取ServletContext对象
System.out.println(config.getServletContext());
}
//注:servlet程序和servletConfig对象都是由Tomcat创建的,我们只是使用。Servlet程序默认是第一次访问的时候创建,ServletConfig是每个Servlet程序创建时,就负责创建一个ServletConfig对象。传给Init
ServletContext类
1、ServletConfig类其实是一个接口,它表示Servlet上下文对象
2、一个web工程只有一个ServletConfig对象
3、ServletContext对象是一个域对象(域对象,是可以像Map一样存取数据的对象,叫做域对象,这里的域指的是存取数据的操作范围。与、域对象也有一说是作用范围是整个工程的对象)
4、ServletContext是在web工程部署启动的时候创建。在web工程停止时销毁。
| 项 | 存数据 | 取数据 | 删除数据 |
|---|---|---|---|
| Map | put() | get() | remove |
| 域对象 | putAttribute() | getAttribute() | removeAttribute() |
ServletContent类的四个常见的作用
1、获取web.xml配置的上下文参数context-param
2、获取当前的工程路径,格式:/工程路径
3、获取工程部署后在服务器硬盘上的绝对路径
4、像Map一样存取数据
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>context</param-value>
</context-param>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1、获取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的参数username的值是:"+password);
// 2、获取当前的工程路径,格式:/工程路径
String contextPath = context.getContextPath();
System.out.println("获取当前工程路径"+contextPath);
// 3、获取工程部署后在服务器硬盘上的绝对路径
//斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到IDEA代码的web目录
String realPath = context.getRealPath("/");
System.out.println("工程部署的路径是:"+realPath);
realPath = context.getRealPath("/css");
System.out.println("css的绝对路径是:"+realPath);
realPath = context.getRealPath("/img");
System.out.println("img的绝对路径是:"+realPath);
}
