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的三种方式

  1. 实现接口Servlet
  2. 继承GenericServlet(要处理前端的请求的话还需要识别是哪一种请求)
  3. 继承HttpServlet方法(推荐使用)

Servlet的相关配置

urlpartten:Servlet访问路径
1. 一个Servlet可以定义多个访问路径 :

@WebServlet({"/d4","/demo4","/dd4"})
  1. 路径定义规则:
    /xxx:路径匹配
@WebServlet("/demo4")

/xxx/xxx:多层路径,目录结构

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

/*的访问级别是特别低的,当别的都访问不到了才会访问它

@WebServlet("/*")
  1. *.do:扩展名匹配
@WebServlet("*.do")

访问的时候可以是localhost:8080/demo4.do,注意*前面不要加/

servlet的体系结构相关推荐

  1. Servlet的体系结构 || Servlet相关配置

    Servlet的体系结构         Servlet -- 接口         |     GenericServlet -- 抽象类         |     HttpServlet  -- ...

  2. [JavaWeb-Servlet]Servlet的体系结构

    Servlet的体系结构 Servlet -- 接口|GenericServlet -- 抽象类|HttpServlet -- 抽象类* GenericServlet:将Servlet接口中其他的方法 ...

  3. Servlet的体系结构(一张图清晰明了)

  4. Servlet和HTTP请求协议-学习笔记02【Servlet_体系结构与urlpartten配置、HTTP请求协议】

    Java后端 学习路线 笔记汇总表[黑马程序员] Servlet和HTTP请求协议-学习笔记01[Servlet_快速入门-生命周期方法.Servlet_3.0注解配置.IDEA与tomcat相关配置 ...

  5. 10 Java程序员面试宝典视频课程之Servlet

    一.http 全称是什么? 有什么作用? 答: 1.超文本传输协议(HTTP:Hypertext Transport Protocol)是万维网应用层的协议,它通过两个程序实现:一个是客户端程序(各种 ...

  6. Servlet、Http、Request

    2019独角兽企业重金招聘Python工程师标准>>> Servlet: 1. 概念 2. 步骤 3. 执行原理 4. 生命周期 5. Servlet3.0 注解配置 6. Serv ...

  7. Servlet 工作原理解析

    2019独角兽企业重金招聘Python工程师标准>>> 从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servle ...

  8. 【Java】Servlet 工作原理解析

    Web 技术成为当今主流的互联网 Web 应用技术之一,而 Servlet 是 Java Web 技术的核心基础.因而掌握 Servlet 的工作原理是成为一名合格的 Java Web 技术开发人员的 ...

  9. Servlet 工作原理解析--转载

    原文:http://www.ibm.com/developerworks/cn/java/j-lo-servlet/index.html?ca=drs- Web 技术成为当今主流的互联网 Web 应用 ...

最新文章

  1. Java基础学习(1)
  2. logistics-6-decidedZone management
  3. Verilog中wire与reg类型的区别
  4. hexo右下角弄一个live2d的卡通动画小人
  5. Ayoub's function CodeForces - 1301C(组合数学)
  6. Oracle查看锁表
  7. 毕业论文排版之Word 中公式居中,编号靠右该怎么设置(针对左右不对称页边距)
  8. 聚焦国内名企开源!OSCAR 开源先锋日(1020)全部议程首次曝光
  9. 路由器长期通电好不好?
  10. CAD2020操作手册
  11. 利用Python实现财务分析/经营分析自动化
  12. HTTP协议与内容压缩(HTTP协议详解)
  13. 佳能7660cdn 评价_佳能Canon LBP7660Cdn 驱动
  14. 美团设计模式在外卖营销业务中的实践-学习笔记(一)
  15. MIUI12 Google play无法下载chrome及没有快捷方式的解决方法
  16. java jframe 切换_java – 在JFrame中切换面板
  17. Netty 警告 The pipeline contains no upstream handlers; discarding:
  18. 租房系统源码(仅供参考)
  19. 新唐 N76E003 单片机8051汇编 PWM测试程序
  20. 软件测试流程及主要内容

热门文章

  1. Python中的堆实现:heapq 模块——利用堆结构实现快速访问数据流中的中位数
  2. 利用 Sql 实现数据透视表功能
  3. 房天下数据爬取及简单数据分析
  4. 什么是 SAP Commerce Cloud 的 Paragraph component
  5. 通过 SAP UI5 的 TypeScript 开发环境,来学习什么是 DefinitelyTyped
  6. Github continuous deployment (CD) 最佳实践
  7. 调试Angular指令实现时一个有用的内部属性:__ngContext__
  8. 一个介绍SAP git-enabled CTS的视频
  9. Angular如何响应DOM event
  10. 如何找到ABAP里被动态调用的update function module