servlet的体系结构
生活随笔
收集整理的這篇文章主要介紹了
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方法:
通過瀏覽器直接請求是get方式,所以打印doget
定義一個login.html,寫一個post的請求
重新啟動服務器,訪問http://localhost:8080/login.html
輸入文本后提交然后跳轉到/demo3的頁面,控制臺打印dopost
如果將請求方式改為get,跳轉到/demo3的url是帶參數的,控制臺打印doget
總結:定義Servlet的三種方式
Servlet的相關配置
urlpartten:Servlet訪問路徑
1. 一個Servlet可以定義多個訪問路徑 :
/xxx:路徑匹配
/xxx/xxx:多層路徑,目錄結構
@WebServlet("/user/demo4") @WebServlet("/user/*")/*的訪問級別是特別低的,當別的都訪問不到了才會訪問它
@WebServlet("/*")訪問的時候可以是localhost:8080/demo4.do,注意*前面不要加/
總結
以上是生活随笔為你收集整理的servlet的体系结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 空调开什么模式适合宝宝(如何选择空调)
- 下一篇: 数据结构树及相关算法题