概念
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 {
}
}