概念
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 {
}
}
标签:ServletException,Servlet,博客,一遍,IOException,import,servlet,javax
From: https://www.cnblogs.com/he-cheng/p/17269573.html