Writer :BYSocket(泥沙砖瓦浆木匠)html
微 博:BYSocketjava
豆 瓣:BYSocketgit
FaceBook:BYSocketgithub
Twitter :BYSocketweb
“眨眼间,离上一篇写技术博文时隔1个月。怕本身真的生疏了,都是备案太慢惹得。哈哈,继续high~ ”面试
从[JavaEE 要懂的小事] Http相关 ,一直想写点Web开发相关的。最近项目接口开发紧,还有准备新的九月份战斗。JDK IO源码就隔一段落,温故知新看看Servlet & JSP 相关。把本身基础累积回顾一遍,并和你们分享分享一些心得和代码。这里应该涉及到一部分源码,开发思想和一些手工作出的图。喜欢java,或者有必定Java开发经验的多提宝贵意见。
express
从事web开发的人,会很清楚一个东西叫HTTP服务器,好比JEE开发—Tomcat,Jetty,.NET开发—ISS等。HTTP服务器是使用 HTTP(超文本传输协议) 与客户机浏览器进行信息交流。下面就是HTTP服务器简单交互图:(来自[JavaEE 要懂的小事] Http相关 博客)apache
HTTP服务器是Web服务器的一种,也是开发最多见的,天然还有其余方式进行信息交互,好比FTP文件服务器…
小程序
Web服务器是能够向发出请求的浏览器提供文档的程序。其核心过程为api
链接过程 — 请求过程 — 应答过程 — 关闭链接
这让我想到了Tomcat架构的一张图:
如图,Tomcat 包含了核心服务模块:Connector链接模块 和 Container 容器。Tomcat Server 核心是一个 Servlet/JSP Container。对每个HTTP请求,过程以下
— 获取链接
— Servlet来分析请求(HttpServletRequest)
— 调用其service方法,进行业务处理
— 产生相应的响应(HttpServletResponse)
— 关闭链接
如图:
蓝色线指向过程是请求,绿色线指向过程是响应过程。也就是上面Web服务器核心过程:“链接过程 — 请求过程 — 应答过程 — 关闭链接”
什么是Servlet?(每次都会不停的问本身,这是什么“What”?紧接着应该是什么用“How”吧)
在 JavaEE 6文档中,介绍以下
“Servlet 是运行在Web服务器的Java小程序。Servlet能够获取并针对Web客户端的请求做出响应。通常状况下,经过HTTP,即超文本传输协议,进行传输通讯。”
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
因此,Servlet 是Web服务器核心工做的抽象。它不仅仅只是实现HttpServlet,可能实现有FtpServlet(这个我猜的)等。相对较多的Web开发,知道的确定是HttpServlet。
在 JavaEE 6文档中,是这样介绍HttpServlet:
“HttpServlet 提供了一个能被继承后建立一个适应Web网站的Http Servlet的抽象类。”
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.
光说不练假把式,练一个“Hello,Servlet/JSP World!”:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /* * Copyright [2015] [Jeff Lee] * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @author Jeff Lee * @since 2015-6-25 19:46:45 * HelloWrold案例 */ @WebServlet(urlPatterns = "/helloWorld.html") public class HelloWorldServletT extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ // 获取输出打印对象 PrintWriter out = resp.getWriter(); out.println("Hello,Servlet/JSP World!"); } }
右键该HelloWorldServletT.java文件 — Run As — Run On Server — 选择Tomcat服务器 — Finish便可
等待片刻,你可看到网页上以下输出。这就是客户端从HttpServlet获取到的响应:
休息一下吧~ 看看小广告:
开源代码都在个人gitHub上哦 — https://github.com/JeffLi1993
@WebServlet(urlPatterns = "/helloWorld.html")
@WebServlet 注解用于声明一个HttpServlet的配置。其中,urlPatters = “/helloWorld.html”,urlPatterns复数形式,说明至少一个URL必须被申明。它和另外一个value必须存在一个,但不能同时存在。若是要匹配多个URL路径的话,以下:
@WebServlet(urlPatterns = { "/helloWorld01.html", "/helloWorld02.html" }
下面有个@Override,重写了父类HttpServlet的doGet方法。咱们先看看父类HttpServlet。HttpServlet是一个抽象类,它提供了如下方法:
— doGet , 服务于 HTPP GET 请求
— doPost , 服务于 HTTP POST 请求
— doPut , 服务于 HTTP PUT 请求
— doDelete,服务于 HTTP DELETE 请求
…
如图:
对于不一样的请求,HttpServlet的子类必须相应的实现至少一个方法,一般来讲,会是其中一个,这样代码比较清晰。那父类的doGet方法作了什么工做呢?
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_get_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } }
这里就简单的获取了下HTTP协议及Http Local信息,而后能够协议是不是1.1,作出分别是405或者400HTTP状态码的响应。
回到HelloWorldServletT.java 这里:
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取输出打印对象 PrintWriter out = resp.getWriter(); out.println("Hello,Servlet/JSP World!"); }
表示该HelloWorldServletT会接受Http GET请求,并OOM到HttpServletRequest,并执行里面的逻辑代码和返回响应。 这里从HttpServletResponse对象中获取到输出打印对象PrintWriter,而后输出了“Hello,Servlet/JSP World!”。
完毕!哦还有一点补充补充补充:
print,这里还好一句话。若是打印个table会很麻烦,所以有一个JSP的东西出现了,是Servlet的HTML化身。
又回到这个简单的 Get Servlet代码:
public class HelloWorldServletT extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ // 获取输出打印对象 PrintWriter out = resp.getWriter(); out.println("Hello,Servlet/JSP World!"); } }
这过程总结以下:
— 从浏览器(Client)获取链接”/helloWorld.html”
— Tomcat Connector模块将请求(Request)传递给 Container模块
— Container 模块会作如下事情
—— 分析HTPP请求信息,组装成HttpServletRequest对象
—— 建立新的HttpServletResponse对象
—— 根据路由配置,搜索相应的Servlet,并建立一个线程用于处理本次请求。此时线程会将上面Request和Response对象的索引,传递给Servlet
— 新线程中的Servlet处理逻辑
— 线程结束后,经过HttpServletResponse对象的PrintWriter,返回浏览器一个信息
过程图以下:
蓝色线指向过程是请求,绿色线指向过程是响应过程,橙色线指向过程是内部处理过程。
有些面试题会这样问:
Servlet是线程安全的吗?
不是,一个servlet实现类只会有一个实例对象,多个线程是可能会访问同一个servlet实例对象的,线程安全问题都是由全局变量及静态变量引发的。
所以,Servlet对象实例化是在以第一次请求此Servlet时,若是访问后,实例对象存在内存中,只会在服务器中止时,它才会消失。它不会随着各个线程结束而结束。所以下次访问Servlet时,Servlet Container会搜索相应的Servlet,若是不存在,Container新建相应的Servlet。这也是咱们想要的结果。
发现这一博客写的太多,回头一看。能够写成三个文章了。言归正传本文要点以下
一、简单介绍Web服务器 及 Tomcat容器
二、第一个Sevlet的开发及使用
三、深刻源码及api介绍使用
四、总结一次请求及响应的真实过程
五、欢迎点击个人博客及GitHub — 博客提供RSS订阅哦
———- http://www.bysocket.com/ ————- https://github.com/JeffLi1993 ———-