java 基础 --- servlet
问题 :
- servlet 这个类是有什么作用
概述
servlet 是个接口,这个接口的作用是规范了接收请求的处理类。而最终的实现交给了 servlet 容器去实现。
servlet 接口
接口方法如下 :
public interface Servlet {
void init(ServletConfig var1) throws ServletException; ServletConfig getServletConfig(); void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException; String getServletInfo(); void destroy();
}
其中最重要的是 service , init , destory 方法了。
servlet 与 Tomcat 的关系
Servlet运行于支持Java的应用服务器中。从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。
由容器来封装请求到 servlet 中,由容器来管理 servlet 。 下面一图可以看到容器处理请求时和 servlet 的交互。
Tomcat 管理 servlet
Tomcat 与 servlet 的交互可以看这一篇文章。
As managed components, servlets have a life cycle, which begins when the managing container loads the servlet class, usually in response to a request, and ends when the container closes the servlet by calling the "destroy" method. All the servlet's activity between these two points is considered part of its life cycle.
The lifecycle of a typical servlet running on Tomcat might look something like this:
- Tomcat receives a request from a client through one of its connectors.
- Tomcat maps this request to the appropriate Engine for processing. These Engines are contained within other elements, such as Hosts and Servers, which limit the scope of Tomcat's search for the correct Engine.
- Once the request has been mapped to the appropriate servlet, Tomcat checks to see if that servlet class has been loaded. If it has not, Tomcat compiles the servlet into Java bytecode, which is executable by the JVM, and creates an instance of the servlet.
- Tomcat initializes the servlet by calling its init method. The servlet includes code that is able to read Tomcat configuration files and act accordingly, as well as declare any resources it might need, so that Tomcat can create them in an orderly, managed fashion.
- Once the servlet has been initialized, Tomcat can call the servlet's service method to process the request, which will be returned as a response.
- During the servlet's lifecycle, Tomcat and the servlet can communicate through the use of listener classes, which monitor the servlet for a variety of state changes. Tomcat can retrieve and store these state changes in a variety of ways, and allow other servlets access to them, allowing state to be maintained and accessed by various components of a given context across the span of a single or multiple user sessions. An example of this functionality in action is an e-commerce application that remembers what the user has added to their cart and is able to pass this data to a checkout process.
- Tomcat calls the servlet's destroy method to smoothly remove the servlet. This action is triggered either by a state change that is being listened for, or by an external command delivered to Tomcat to undeploy the servlet's Context or shut down the server.
上面这段引用加了下划线,简述了大概的过程,同时黄体字表示的是 servlet 接口中定义的方法。结合上面接口的方法方便我们理解整个过程。
补充
redirect 和 forward 的区别
根据转发方式的不同,可以区分为直接请求转发(Forward)和间接请求转发(Redirect).那么两者的区别是什么呢?
Forward和Redirect代表了两种请求转发方式:直接转发和间接转发。对应到代码里,分别是RequestDispatcher类的forward()方法和HttpServletResponse类的sendRedirect()方法。
对于直接方式(forward),客户端浏览器只发出一次请求,Servlet把请求转发给Servlet、HTML、JSP或其它信息资源,由第2个信息资源响应该请求,两个信息资源共享同一个request对象。
对于间接方式(redirect),服务器端在响应第一次请求的时候,让浏览器再向另外一个URL发出请求,从而达到转发的目的。它本质上是 两次HTTP请求,对应两个request对象。
forward 可以直接的访问/WEB-INF里的内容(Spring MVC 内容),而redirect就不能了,再接触一点/WEB-INF的知识点就能更好的了解forward与redirect
总结
- servlet 是个接口规范,具体的实现在服务器(servlet容器)中,文章还介绍了 servlet 与 Tomcat 的交互
- forward 和 redirect 表示两种请求转发的方法,前者为直接转发,前后信息共享一个request , 相当于一个 http 请求。而 redirect 为间接转发,前后信息共为两个不同的request ,相当于两个 http 请求。
参考资料
java 基础 --- servlet的更多相关文章
- Java基础——Servlet
什么是Servlet Servlet是Java Web的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要: l ...
- Java基础——Servlet(一)
在学习Servlet之前,需要首先学习一些关联性的知识. 一.动态网页程序 动态网页:它是网页中的偏功能性的部分也是最重要的部分.它不是我们平时所看见的页面特效,展示的效果.而是,一种交互行为.比如, ...
- Java基础——Servlet(八)文件上传下载
一.简单的文件上传常见的组件Smartupload , Apache 的 commons FileUploadSmartupload上传的步骤: 1.初始化上传上下文 2.准备上传 3.保存文件 &l ...
- Java基础——Servlet(七)过滤器&监听器 相关
一.过滤器简介 Filter 位于客户端和请求资源之间,请求的资源可以是 Servlet Jsp html (img,javascript,css)等.用于拦截浏览器发给服务器的请求(Request) ...
- Java基础——Servlet(五)
哈哈哈...学习Servlet学了半个多月,因为中间有比较灰心的时候,有几天是啥都不学了的状态,看了好几部励志的电影.呃~还是得继续吧.本来计划是好好夯实这里的基础,结果在网找到了介绍比较全面的视频, ...
- Java基础——Servlet(三)
还在学习Servlet,觉得这里的知识点蛮多的.还要继续努力,加油. 拿韩老师的话激励一下自己,共勉.韩老师说,“成功其实也不难,只要树立一个目标,不需要你是一个很强的人,不需要你很高智商,不需要你是 ...
- Java基础——Servlet(六)分页相关
前面写了Servlet(一)到(五),主要是在网上搜罗的视频.对分页这块还是不太清楚.于是有找到一些视频,重新学习了一下.主要是对分页的认识和设计思路.也是为了方便我以后回忆一下.. 一.分页常识 p ...
- Java基础——Servlet(四)
最近一直在学习Servlet,真的有烦躁,一下子要创建好几个文件,服务端.客户端.html页面....学习进度蛮慢的,很容易失掉信心.当学习到cookie时,发现有好多实现是在我们日常生活中可以会遇得 ...
- Java基础——Servlet(二)
好久没有写博客了,最近有在学习.可能是遇到瓶颈了,学到Servlet这里觉得有些吃力.前几天已经学完一遍了,但是学完之后觉得还是很迷茫.去知乎和百度上搜索,遇到的都是一些概念之类的讲解.核心的介绍说, ...
随机推荐
- leetcode 72 编辑距离 JAVA
题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 ...
- G - Ice_cream's world I (并查集)
点击打开链接 ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream ...
- java -io字符流FileWrite操作演示
FileWriter字符输出流演示: /* * FiileWriter 字符流的操作 * FileWriter 的构造方法 可传递 File类型 还可以传递String类型 * * 方法 : * wr ...
- 给洛谷填坑的spj……
这里提供了洛谷某些题的$special\ judge$,供需要的oier拿过去对拍. 1.P3825 #include "testlib.h" using namespace st ...
- jvm字节码简介
1.概述 java虚拟机的指令由一个字节长度的.代表着某种特定操作含义的数字(成为操作码,Opcde)和跟随其后的0到多个此操作所需参数(操作数,Operands).由于操作码的长度为一个字节,所以指 ...
- AssertJ断言系列<一>
1 - Get AssertJ Core assertions Maven的pom.xml加入如下配置: <dependency> <groupId>org.assertj&l ...
- 递归实现进制转换(C++版)
上次呢,我们留下了一道题,今天我们来一起看一看: 题目链接:https://www.cnblogs.com/gaozirong/p/10547434.html 这是我写的程序,大家可以对照参考一下(C ...
- 架构师养成记--29.redis开篇
主要有从下几点讲解 NOSQL(Redis) 简介.redis安装与部署 Redis基础事件类型详解 Redis高级命令 Redis与java的使用 Redis集群搭建 Redis集群与spring的 ...
- (Lua) C++ 加入 Lua 環境擴充應用強度
Lua 在網上有非常多的介紹,就是一個小而巧的語言,可以放入嵌入式系統 也可以在一般的應用上非常強大,這邊主要記錄如何讓Lua加入C++裡頭應用 Lua source code 是以 C 語言下去編寫 ...
- SpringMVC初写(二)映射类型、限制和数据绑定
映射路径 a)映射路径的概述 所谓的映射路径,就是匹配请求路径和执行方法关系的路径 请求路径:http://localhost:8080/springmvc-demo-cofig/say.do 映射路 ...