Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter :BYSocket “眨眼间,离上一篇写技术博文时隔1个月。怕自己真的生疏了,都是备案太慢惹得。哈哈,继续high~ ” 从[JavaEE 要懂的小事] Http相关 ,一直想写点web开发相关的。最近项目接口开发紧,还有准备新的九月份战斗。JDK IO源码就隔一段落,温故知新看看Servlet & JSP 相关。把自己基础累积回顾一遍,并和大家分享分享一些心得和代码。这里应该涉及到一部分源码,开发思想和一些手工做出的图。喜欢java,或者有一定java开发经验的多提宝贵意见。 一、Web服务器 从事web开发的人,会很清楚一个东西叫 Web服务器,比如JEE开发—Tomcat,Jetty,.net开发—ISS等。HTTP服务器是使用 HTTP(超文本传输协议) 与客户机浏览器进行信息交流。下面就是HTTP服务器简单交互图:(来自[JavaEE 要懂的小事] Http相关 博客) HTTP服务器是Web服务器的一种,也是开发最常见的,自然还有其他方式进行信息交互,比如FTP文件服务器… 这让我想到了Tomcat架构的一张图: 二、Tomcat 简单说几句 如图,Tomcat 包含了核心服务模块:Connector连接模块 和 Container 容器。Tomcat Server 核心是一个 Servlet/JSP Container。对每一个HTTP请求,过程如下 如图: 蓝色线指向过程是请求,绿色线指向过程是响应过程。也就是上面Web服务器核心过程:“连接过程 — 请求过程 — 应答过程 — 关闭连接” 三、我第一个Servlet 什么是Servlet?(每次都会不停的问自己,这是什么“What”?紧接着应该是什么用“How”吧) 在 JavaEE 6文档中,介绍如下 所以,Servlet 是Web服务器核心工作的抽象。它不单单只是实现HttpServlet,可能实现有FtpServlet(这个我猜的)等。相对较多的Web开发,知道的肯定是HttpServlet。 在 JavaEE 6文档中,是这样介绍HttpServlet: 光说不练假把式,练一个“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获取到的响应: 三、分析源码 - @WebServlet(urlPatterns = "/helloWorld.html")
复制代码@WebServlet 注解用于声明一个HttpServlet的配置。其中,urlPatters = “/helloWorld.html”,urlPatterns复数形式,说明至少一个URL必须被申明。它和另一个value必须存在一个,但不能同时存在。如果要匹配多个URL路径的话,如下: - @WebServlet(urlPatterns = { "/helloWorld01.html", "/helloWorld02.html" }
复制代码下面有个@Override,重写了父类HttpServlet的doGet方法。我们先看看父类HttpServlet。HttpServlet是一个抽象类,它提供了以下方法: 如图: 对于不同的请求,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!”。 完毕!哦还有一点补充补充补充: 五、深入Servlet 具体过程 又回到这个简单的 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!");
- }
- }
复制代码这过程总结如下: 过程图如下: 蓝色线指向过程是请求,绿色线指向过程是响应过程,橙色线指向过程是内部处理过程。 有些面试题会这样问: 因此,Servlet对象实例化是在以第一次请求此Servlet时,如果访问后,实例对象存在内存中,只会在服务器停止时,它才会消失。它不会随着各个线程结束而结束。因此下次访问Servlet时,Servlet Container会搜索相应的Servlet,如果不存在,Container新建相应的Servlet。这也是我们想要的结果。 再来个恶心的面试题: 六、小结 发现这一博客写的太多,回头一看。可以写成三个文章了。言归正传本文要点如下
|