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. 继承 ...
随机推荐
- laravel上传git如何忽略你不想提交的文件
1.在文件根目录下面有一个文件 .gitignore .gitignore文件用来忽略被指定的文件或文件夹的改动,被记录在.gitignore文件里的文件或文件夹,是无法被git跟踪到的,换句话说,被 ...
- Csla One or more properties are not registered for this type
在实际运行中,好好运行的程序出现了以下问题: 2019-12-27 10:40:00,164 [DefaultQuartzScheduler_Worker-2] ERROR IBeam.BCPool. ...
- 查看Spark与Hadoop等其他组件的兼容版本
安装与Spark相关的其他组件的时候,例如JDK,Hadoop,Yarn,Hive,Kafka等,要考虑到这些组件和Spark的版本兼容关系.这个对应关系可以在Spark源代码的pom.xml文件中查 ...
- 【kuangbin带你飞】 MST专题
唉,被班级合唱和复变考试搞得心力交瘁.新算法学不进去,更新下吧 A - Til the Cows Come Home The Head Elder of the tropical island of ...
- Ionic 使用 NFC
Ionic 使用 NFC 哎哟喂,因为项目需要使用 Ionic 调用手机 NFC 功能,踩了好多坑,真的是,不过终于不负众望拿到了id.现在就记录一下我的步骤和踩过的坑! 步骤 我装的Ionic可能是 ...
- Pytest学习10-pytest与unittest的区别
一.用例编写规则 1.unittest提供了test cases.test suites.test fixtures.test runner相关的类,让测试更加明确.方便.可控.使用unittest编 ...
- Coursera 吴恩达 深度学习 学习笔记
神经网络和深度学习 Week 1-2 神经网络基础 Week 3 浅层神经网络 Week 4 深层神经网络 改善深层神经网络 Week 1 深度学习的实用层面 Week 2 优化算法 Week 3 超 ...
- 改变容器Size后,刷新地图大小。
You need to call the API to update map size. http://dev.openlayers.org/docs/files/OpenLayers/Map-js. ...
- es报错org.frameworkset.elasticsearch.ElasticSearchException: {"error":{"root_cause":[{"type":"cluster_block_exception","reason":"blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"}],
今天es在保存数据的时候报错 org.frameworkset.elasticsearch.ElasticSearchException: {"error":{"root ...
- AntDesign(React)学习-12 使用Table
AntDesign(Vue)版的Table中使用图片https://www.cnblogs.com/zhaogaojian/p/11119762.html 之前在使用VUE版Table时,使用大图片时 ...