servlet原理+流程图+简单实现案例(javaweb)
servlet原理+流程圖+簡單實現(xiàn)案例(javaweb)
1.servlet原理(轉(zhuǎn)載)
容器負責(zé)根據(jù)請求的信息找到對應(yīng)的Servlet,傳遞Request和Response參數(shù),調(diào)用Servlet的service方法,完成請求的響應(yīng)。
Servlet 的運行模式是一個典型的“握手型的交互式”運行模式。所謂“握手型的交互式”就是兩個模塊為了交換數(shù)據(jù)通常都會準備一個交易場景,這個場景一直跟隨個這個交易過程直到這個交易完成為止。這個交易場景的初始化是根據(jù)這次交易對象指定的參數(shù)來定制的,這些指定參數(shù)通常就會是一個配置類。所以對號入座,交易場景就由 ServletContext 來描述,而定制的參數(shù)集合就由 ServletConfig 來描述。而 ServletRequest 和 ServletResponse 就是要交互的具體對象了,它們通常都是作為運輸工具來傳遞交互結(jié)果。
當(dāng)用戶從瀏覽器向服務(wù)器發(fā)起一個請求,通常會包含如下信息:http://hostname: port /contextpath/servletpath,hostname 和 port 是用來與服務(wù)器建立 TCP 連接,而后面的 URL 才是用來選擇服務(wù)器中那個子容器服務(wù)用戶的請求。映射工作有專門一個類來完成的,這個就是 org.apache.tomcat.util.http.mapper,這個類保存了 Tomcat 的 Container 容器中的所有子容器的信息,當(dāng) org.apache.catalina.connector. Request 類在進入 Container 容器之前,mapper 將會根據(jù)這次請求的 hostnane 和 contextpath 將 host 和 context 容器設(shè)置到 Request 的 mappingData 屬性中。所以當(dāng) Request 進入 Container 容器之前,它要訪問那個子容器這時就已經(jīng)確定了。
2.流程圖
3.簡單實現(xiàn)案例
servlet實現(xiàn)類
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("doGet...");PrintWriter writer = resp.getWriter();writer.print("Hello Servlet!");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);} }web.xml
<?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><servlet-name>HelloServlet</servlet-name><servlet-class>com.tang.servlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping> </web-app>index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body> <form action="/hello">賬號: <input type="text" name="zhanghao"><input type="submit"> </form> </body> </html>pom.xml
<?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>com.tang</groupId><artifactId>javaweb-02-maven</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>javaweb-servlet02</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>jakarta.servlet.jsp</groupId><artifactId>jakarta.servlet.jsp-api</artifactId><version>3.0.0</version> <!-- <scope>provided</scope>--></dependency><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>5.0.0</version> <!-- <scope>provided</scope>--></dependency></dependencies> </project>結(jié)果
總結(jié)
以上是生活随笔為你收集整理的servlet原理+流程图+简单实现案例(javaweb)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet概述及其原理
- 下一篇: LQR控制实例、传递函数与状态矩阵相互转