Servlet继承体系结构
Servlet如何只定义1个service方法,其它的方法按需求设置
Servlet——接口
↑继承
GenericServlet——抽象类
↑继承
HttpServlet——抽象类:推荐使用
GenericServlet抽象类:只需要实现service方法
只用重写一个方法,其他的方法都做了空实现(仅声明了方法,方法体内没有内容)
只把service方法做了抽象(abstract)
需要其他方法时重写就行了
虽然方便,但真正开发时不使用。真正使用HttpServlet
//
// 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();
}
}
package cn.itcast.web.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; /**
* @author 旗木五五开
* @create 2020-02-18 20:20
*/
@WebServlet("/demo2")
public class ServletDemo2 extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Demo2来袭");
}
}
HttpServlet抽象类:对HTTP协议的封装,简化操作
为什么会有这个类的出现?
为了屏蔽get和post请求方式处理逻辑(继承HttpServlet,复写方法doGet();dopost();)。
因为将来都是调用service方法,service做一个方法分发,是哪个方式就调用哪个方法
HttpServlet基本的定义原理

实现步骤:
- 定义类继承HttpServlet
- 复写doGet();、dopost();方法
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
// 1.判断请求方式(method对应的请求方式)
String method=req.getMethod();
long lastModified;
if(method.equals("GET")){//常量放前面可以防止空指针异常
lastModified=this.getLastModified(req);
if(lastModified==-1L){
this.doGet(req,resp);//调用doGet方法
}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")){//HEAD
lastModified=this.getLastModified(req);
this.maybeSetLastModified(resp,lastModified);
this.doHead(req,resp);
}else if(method.equals("POST")){//POST
this.doPost(req,resp);
}else if(method.equals("PUT")){//PUT
this.doPut(req,resp);
}else if(method.equals("DELETE")){//DELETE
this.doDelete(req,resp);
}else if(method.equals("OPTIONS")){//OPTIONS
this.doOptions(req,resp);
}else if(method.equals("TRACE")){//TRACE
//以上对应HTTP7个请求方式。只需要关心两个常用的就行get和post
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);
}
}
练习
package cn.itcast.web.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; /**
* @author 旗木五五开
* @create 2020-02-18 23:44
*/
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("dogat");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("dopost");
}
}
通过浏览器直接请求是get方式


通过表单完成post请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="/demo3" method="post">
<input type="text" name="username">
<br>
<input type="submit" value="提交">
</form> </body>
</html>


Servlet继承体系结构的更多相关文章
- [03] Servlet继承关系和生命周期
1.Servlet的继承关系 假如现有我们自定义的一个Servlet,继承HttpServlet,那么实际上它的继承链如下图: 可以看到,核心的部分在于: 两个顶级接口 Servlet Servl ...
- 最简单的Servlet继承HttpServlet查询数据库登录验证
<%-- Created by IntelliJ IDEA. User: yunqing Date: 2017-12-06 Time: 9:11 To change this template ...
- servlet、filter、listener继承的基类和获得作用域的方式
一.servlet: 1.servlet属于j2ee的组件,构建servlet的web project不需要导入项目框架jar包 2.servlet的体系结构: 在j2ee API中,提供给serv ...
- Java 之 Servlet 体系结构
Servlet 的体系结构 体系结构示意图: 1.Servlet 接口 如果直接实现这个接口,需要重写里面所有的方法,但是只需要使用 service() 方法,其他的不常用. 2.GenericSer ...
- Java Servlet详解(体系结构+注解配置+生命周期)
Java Servlet详解(注解配置+生命周期) 什么是Servlet : (Server applet)? 顾名思义:服务端的小程序 Servlet只是一个接口,定义了Java被浏览器访问到(To ...
- Servlet 工作原理解析
转自:http://www.ibm.com/developerworks/cn/java/j-lo-servlet/ Web 技术成为当今主流的互联网 Web 应用技术之一,而 Servlet 是 J ...
- JavaWeb总结--Servlet 工作原理解析
从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servlet 容器的关系有点像枪和子弹的关系,枪是为子弹而生,而子弹又让枪有了杀伤力 ...
- [Java拾遗三]JavaWeb基础之Servlet
Servlet 1,servlet介绍 servlet是一项动态web资源开发技术. 运行在服务器端. 作用:处理业务逻辑,生成动态的内容,返回给浏览器 ...
- Servlet、JSP选择题(2)
Java EE软件工程师认证考试 试题库-选择题 一. 选择题(包括单选和双选) 1.B 编写一个Filter,需要( ) A. 继承Filter 类 B. 实现Filter 接口 C. 继承 ...
随机推荐
- Visual Studio Code搭建Python开发环境方法总结
更新:目前VSCode官方Python插件已经支持代码运行与调试,无需安装Code Runner插件. 1.下载安装Python,地址 https://www.python.org/downloads ...
- 从0开始学算法--排序(1.12c++利用标准库排序)
1,简单数组按升序排序 sort(a,a+n); #include <algorithm> #include <iostream> #include <cstring&g ...
- ansible-主机分组
一.安装ansible yum install ansible -y ansible --version //查看版本,没有报错即安装成功 二.ansible主机定义与分组 1. ansible配置文 ...
- php7 安装redis拓展
配置之前应该是环境已经搭好了,phpinfo的页面可以加载出来. 使用git clone下载git上的phpredis扩展包 git clone https://github.com/phpre ...
- ActiveMQ的p2p模式与发布订阅模式
1.消息中间件:采用异步通讯防止,支持点对点以及发布订阅模式,可以解决高并发问题 传统调用接口,可能发生阻塞,重复提交,超时等等问题,可以利用消息中间件发送异步通讯请求 ...
- 使用wsgiref手撸web框架
模板 前言 要说到应用程序,就不得不提的就是cs架构和BS架构 所谓的cs架构就是client端和server端,就像我们的电脑上的qq,微信等应用程序 bs架构就是浏览器端和server端,我们不需 ...
- 【Unity|C#】基础篇(7)——属性(Property)/ 索引器(Indexer)
[学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...
- hadoop搭建HA集群之后不能自动切换namenode
在搭好HA集群之后,想测试一下集群的高可用性,于是先把active的namenode给停掉: hadoop-daemon.sh stop namenode 或者直接kill掉该节点namenode的对 ...
- Exception in thread "http-apr-8080-exec-1" java.lang.StackOverflowError
Exception in thread "http-apr-8080-exec-1" java.lang.StackOverflowError 可能执行了递归,陷入了死循环 如下我 ...
- docker usage (2)
1. docker command docker start postgres docker container ls --all docker image ls --all docker ps -a ...