概念
Servlet 其实就是 Java 提供的一门动态 web 资源开发技术。本质就是一个接口。
快速入门
创建 web 项目,导入 servlet 依赖坐标(注意依赖范围 scope,是 provided,只在编译和测试时使用)
pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>tomcat-demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- <configuration>-->
<!-- <port>80</port><!– 访问端口号 –>-->
<!-- <path>/</path><!– 项目访问路径 –>-->
<!-- </configuration>-->
</plugin></plugins>
</build>
<packaging>war</packaging>
</project>
复制代码
创建:定义一个类,实现 servlet 接口,重写接口中的方法在 Java 目录下创建类,类名后加入 implements Servlet 实现接口,再 CTRL+o 重写父类方法。
配置:在类上使用 @WebServlet 注解,配置该 servlet 的访问路径再在类名写写明路径的注解。2、3 步代码如下:
package com.itheijma.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo1")
public class ServletDemo1 implements Servlet {
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet hello world");
}
public String getServletInfo() {
return null;
}
public void destroy() {
}
public void init(ServletConfig servletConfig) throws ServletException {
}
public ServletConfig getServletConfig() {
return null;
}
}
复制代码
访问,启动 tomcat,在浏览器中访问随后在网页中访问 demo1,控制台出现要输出的信息,说明 servlet 编写成功。
Servlet 执行流程
浏览器根据路径向服务器发出请求,服务器再根据我们自定义的 Servlet 接口创建 Servlet 对象,再调用 service 方法,执行我们的代码。
Servlet 生命周期
对象的生命周期指一个对象从被创建到销毁的整个过程。servlet 运行在 servlet 容器中,其生命周期由容器来管理,分为四个阶段:
加载和实例化:默认情况下,当 servlet 第一次被访问时,由容器创建 servlet 对象。
初始化:在 servlet 实例化之后,容器调用 servlet 的 init()方法初始化这个对象。完成一些如加载配置文件、创建连接等初始化的工作。注:该方法只被调用一次,因为一个 servlet 对象只被创建一次。
请求处理:每次请求 servlet 时,servlet 容器都会调用 servlet 的 service()方法对请求进行处理。
服务终止:当需要释放内存或容器关闭时,容器就会调用 servlet 实例的 destory()方法完成资源的释放。
package com.itheijma.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet(urlPatterns = "/demo2",loadOnStartup = 1)
public class ServletDemo2 implements Servlet {
/**
* 初始化方法
* 1. 调用时机:默认情况下,servlet被第一次访问时调用
* * loadOnStartup
* 2. 调用次数:1次
* @param servletConfig
* @throws ServletException
*/
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init....");
}
/**
* 提供服务方法
* 1. 调用时机:每一次servlet被访问时调用
* 2. 调用次数:多次
*
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet hello world");
}
/**
* 销毁方法
* 1. 调用时机:内存释放或服务器关闭的时候,servlet对象会被销毁,调用
* 2. 调用次数:一次
*/
public void destroy() {
System.out.println("destory.....");
}
public String getServletInfo() {
return null;
}
public ServletConfig getServletConfig() {
return null;
}
}
复制代码
Servlet 体系结构
Servlet——Servlet 体系根接口 GenericServlet——Servlet 抽象实现类 HttpServlet——对 HTTP 协议封装的 Servlet 实现类因为我们开发的是 B/S 架构的 web 项目,都是针对 HTTP 协议的,所以我们自定义的 Servlet,会继承 HttpServlet
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post...");
}
}
复制代码
urlPattern 配置
Servlet 想要被访问,必须配置其访问路径(urlPattern)
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* urlPattern: 一个Servlet可以配置多个访问路径
*/
@WebServlet(urlPatterns = {"/demo4","/demo5"})
public class ServletDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo4 get.....");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
复制代码
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* urlPattern
* *目录匹配 /user/*
*/
@WebServlet(urlPatterns = "/user/*")
public class ServletDemo6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo6 get.....");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
复制代码
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* urlPattern
* *扩展名匹配: *.do
*/
@WebServlet(urlPatterns = "*.do")
public class ServletDemo7 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo7 get.....");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
复制代码
package com.itheijma.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* urlPattern
* *任意匹配 /* /
*/
@WebServlet(urlPatterns = "/")
public class ServletDemo8 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo8 get.....");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
复制代码
idea 模板创建 Servlet
文章转载自:鹤城
原文链接:https://www.cnblogs.com/he-cheng/p/17269573.html
评论