日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

servlet的体系结构

發布時間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 servlet的体系结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Servlet的體系結構

GenericServlet:將Servlet接口中其他的方法做了默認空實現,只將service()方法作為抽象

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) //package javax.servlet;import java.io.IOException; import java.io.Serializable; import java.util.Enumeration;public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {private static final long serialVersionUID = 1L;private transient ServletConfig config;public GenericServlet() {}public void destroy() {}public String getInitParameter(String name) {return this.getServletConfig().getInitParameter(name);}public Enumeration<String> getInitParameterNames() {return this.getServletConfig().getInitParameterNames();}public ServletConfig getServletConfig() {return this.config;}public ServletContext getServletContext() {return this.getServletConfig().getServletContext();}public String getServletInfo() {return "";}public void init(ServletConfig config) throws ServletException {this.config = config;this.init();}public void init() throws ServletException {}public void log(String msg) {this.getServletContext().log(this.getServletName() + ": " + msg);}public void log(String message, Throwable t) {this.getServletContext().log(this.getServletName() + ": " + message, t);}public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;public String getServletName() {return this.config.getServletName();} }

將來定義Servlet類時,可以繼承GenericServlet,實現service()方法即可.其他方法可以自愿實現。

package com.yuaxninyi.servlet;import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import java.io.IOException;@WebServlet("/demo2") public class ServletDemo2 extends GenericServlet {@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println("demo2");} }

HttpServlet:對http協議的一種封裝,簡化操作

HttpServet源碼中的service方法:

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String method = req.getMethod();long lastModified;if (method.equals("GET")) {lastModified = this.getLastModified(req);if (lastModified == -1L) {this.doGet(req, resp);} else {long ifModifiedSince;try {ifModifiedSince = req.getDateHeader("If-Modified-Since");} catch (IllegalArgumentException var9) {ifModifiedSince = -1L;}if (ifModifiedSince < lastModified / 1000L * 1000L) {this.maybeSetLastModified(resp, lastModified);this.doGet(req, resp);} else {resp.setStatus(304);}}} else if (method.equals("HEAD")) {lastModified = this.getLastModified(req);this.maybeSetLastModified(resp, lastModified);this.doHead(req, resp);} else if (method.equals("POST")) {this.doPost(req, resp);} else if (method.equals("PUT")) {this.doPut(req, resp);} else if (method.equals("DELETE")) {this.doDelete(req, resp);} else if (method.equals("OPTIONS")) {this.doOptions(req, resp);} else if (method.equals("TRACE")) {this.doTrace(req, resp);} else {String errMsg = lStrings.getString("http.method_not_implemented");Object[] errArgs = new Object[]{method};errMsg = MessageFormat.format(errMsg, errArgs);resp.sendError(501, errMsg);}} 1. 定義類繼承HttpServlet2. 復寫doGet/doPost方法 package com.yuaxninyi.servlet;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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("doget");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dopost");} }

通過瀏覽器直接請求是get方式,所以打印doget
定義一個login.html,寫一個post的請求

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><form action="/demo3" method="post"><input name="username"><input type="submit" value="提交"></form></body> </html>

重新啟動服務器,訪問http://localhost:8080/login.html
輸入文本后提交然后跳轉到/demo3的頁面,控制臺打印dopost

如果將請求方式改為get,跳轉到/demo3的url是帶參數的,控制臺打印doget

總結:定義Servlet的三種方式

  • 實現接口Servlet
  • 繼承GenericServlet(要處理前端的請求的話還需要識別是哪一種請求)
  • 繼承HttpServlet方法(推薦使用)
  • Servlet的相關配置

    urlpartten:Servlet訪問路徑
    1. 一個Servlet可以定義多個訪問路徑 :

    @WebServlet({"/d4","/demo4","/dd4"})
  • 路徑定義規則:
    /xxx:路徑匹配
  • @WebServlet("/demo4")

    /xxx/xxx:多層路徑,目錄結構

    @WebServlet("/user/demo4") @WebServlet("/user/*")

    /*的訪問級別是特別低的,當別的都訪問不到了才會訪問它

    @WebServlet("/*")
  • *.do:擴展名匹配
  • @WebServlet("*.do")

    訪問的時候可以是localhost:8080/demo4.do,注意*前面不要加/

    總結

    以上是生活随笔為你收集整理的servlet的体系结构的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。