Servlet版本
Oracle將Java EE(Java SE還自己保留)交給開源組織,Eclipse基金會接手。但Oracle不允許開源組織使用Java名號,所以Jakarta EE名稱於2018.02.26應運而生。
正是因為組織變化,Servlet被割裂為了2個版本,javax.servlet
和jakarta.servlet
。
javax.servlet
已經停止維護,但它仍然是一個非常有用和重要的技術,特別是在許多現有專案中,學習和使用它將為你提供堅實的Web開發基礎。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
如果希望使用Jakarta EE 9或更高版本的Servlet API,則需要切換到javax.servlet
。
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
本文將基於javax.servlet
來介紹Servlet,從Usages來看,它仍然是使用最廣泛的。
Servlet API
Servlet API主要有2個包,javax.servlet
包含了servlet和web container使用的介面和類。javax.servlet.http
包含了http相關的介面和類。
javax.servlet
介面列表:
- Servlet
- ServletRequest
- ServletResponse
- RequestDispatcher
- ServletConfig
- ServletContext
- SingleThreadModel
- Filter
- FilterConfig
- FilterChain
- ServletRequestListener
- ServletRequestAttributeListener
- ServletContextListener
- ServletContextAttributeListener
javax.servlet
類列表:
- GenericServlet
- ServletInputStream
- ServletOutputStream
- ServletRequestWrapper
- ServletResponseWrapper
- ServletRequestEvent
- ServletContextEvent
- ServletRequestAttributeEvent
- ServletContextAttributeEvent
- ServletException
- UnavailableException
javax.servlet.http
介面列表:
- HttpServletRequest
- HttpServletResponse
- HttpSession
- HttpSessionListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- HttpSessionActivationListener
- HttpSessionContext (deprecated now)
javax.servlet.http
類列表:
- HttpServlet
- Cookie
- HttpServletRequestWrapper
- HttpServletResponseWrapper
- HttpSessionEvent
- HttpSessionBindingEvent
- HttpUtils (deprecated now)
Servlet介面
Servlet Interface定義了所有servlet必須具有的行為。主要方法如下:
-
public void init(ServletConfig config) 初始化,只會被web container呼叫1次
-
public void service(ServletRequest request, ServletResponse response) 接收請求,返回響應,每次請求都會呼叫1次
-
public void destroy() 銷燬,只會被web container呼叫1次
-
public ServletConfig getServletConfig() Servlet配置
-
public String getServletInfo() Servlet資訊
以下是程式碼示例:
import java.io.*;
import javax.servlet.*;
public class First implements Servlet {
ServletConfig config = null;
/**
* 初始化
* @param config
*/
public void init(ServletConfig config) {
this.config = config;
System.out.println("servlet is initialized");
}
/**
* 服務
* @param req
* @param res
* @throws IOException
* @throws ServletException
*/
public void service(ServletRequest req, ServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><body>");
out.print("<b>hello simple servlet</b>");
out.print("</body></html>");
}
/**
* 銷燬
*/
public void destroy() {
System.out.println("servlet is destroyed");
}
/**
* 配置
* @return
*/
public ServletConfig getServletConfig() {
return config;
}
/**
* 資訊
* @return
*/
public String getServletInfo() {
return "copyright 2007-1010";
}
}
GenericServlet類
GenericServlet是個抽象類,實現了Servlet, ServletConfig, Serializable介面,能處理任何請求,支援任何協議。主要方法如下:
- public void init(ServletConfig config) 初始化
- public abstract void service(ServletRequest request, ServletResponse response) 接收請求,返回響應,每次請求都會呼叫1次
- public void destroy() 銷燬,只會被web container呼叫1次
- public ServletConfig getServletConfig() Servlet配置
- public String getServletInfo() Servlet資訊
- public void init() 無參初始化
- public ServletContext getServletContext() Servlet上下文
- public String getInitParameter(String name) 根據引數name返回value
- public Enumeration getInitParameterNames() web.xml所有引數
- public String getServletName() Servlet名稱
- public void log(String msg) 記錄Servlet日誌
- public void log(String msg,Throwable t) 記錄Servlet日誌和異常堆疊
以下是程式碼示例:
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class First extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</body></html>");
}
}
HttpServlet類
HttpServlet繼承了GenericServlet抽象類。主要方法如下:
-
public void service(ServletRequest req,ServletResponse res) dispatches the request to the protected service method by converting the request and response object into http type.(這裡結合英文解釋比較清楚,轉換型別後,呼叫第2個service)
-
protected void service(HttpServletRequest req, HttpServletResponse res) receives the request from the service method, and dispatches the request to the doXXX() method depending on the incoming http request type.(這裡結合英文解釋比較清楚,根據不同method,呼叫doXXX()方法)
-
protected void doGet(HttpServletRequest req, HttpServletResponse res) 處理GET請求,web container呼叫
-
protected void doPost(HttpServletRequest req, HttpServletResponse res) 處理POST請求,web container呼叫
-
protected void doHead(HttpServletRequest req, HttpServletResponse res) 處理HEAD請求,web container呼叫
-
protected void doOptions(HttpServletRequest req, HttpServletResponse res) 處理OPTIONS請求,web container呼叫
-
protected void doPut(HttpServletRequest req, HttpServletResponse res) 處理PUT請求,web container呼叫
-
protected void doTrace(HttpServletRequest req, HttpServletResponse res) 處理TRACE請求,web container呼叫
-
protected void doDelete(HttpServletRequest req, HttpServletResponse res) 處理DELETE請求,web container呼叫
-
protected long getLastModified(HttpServletRequest req) 上次修改時間
如果想深入學習Servlet API,可以在Maven pom.xml引入servlet包依賴,External Libraries檢視原始碼。
參考資料:
https://www.javatpoint.com/servlet-api
https://www.javatpoint.com/Servlet-interface
https://www.javatpoint.com/GenericServlet-class
https://www.javatpoint.com/HttpServlet-class
ChatGPT