在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

图解 & 深入浅出 JavaWeb:Servlet必会必知

2017-2-7 13:39| 发布者: zhangjf| 查看: 666| 评论: 0

摘要: Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter :BYSocket “眨眼间,离上一篇写技术博文时隔1个月。怕自己真的生疏了,都 ...

Writer :BYSocket(泥沙砖瓦浆木匠)

微 博:BYSocket

豆 瓣:BYSocket

FaceBook:BYSocket

Twitter :BYSocket

“眨眼间,离上一篇写技术博文时隔1个月。怕自己真的生疏了,都是备案太慢惹得。哈哈,继续high~ ”

从[JavaEE 要懂的小事] Http相关 ,一直想写点web开发相关的。最近项目接口开发紧,还有准备新的九月份战斗。JDK IO源码就隔一段落,温故知新看看Servlet & JSP 相关。把自己基础累积回顾一遍,并和大家分享分享一些心得和代码。这里应该涉及到一部分源码,开发思想和一些手工做出的图。喜欢java,或者有一定java开发经验的多提宝贵意见。

一、Web服务器

从事web开发的人,会很清楚一个东西叫 Web服务器,比如JEE开发—TomcatJetty.net开发ISS等。HTTP服务器是使用 HTTP(超文本传输协议) 与客户机浏览器进行信息交流。下面就是HTTP服务器简单交互图:(来自[JavaEE 要懂的小事] Http相关 博客)

图解 & 深入浅出 JavaWeb:Servlet必会必知

HTTP服务器Web服务器的一种,也是开发最常见的,自然还有其他方式进行信息交互,比如FTP文件服务器

这让我想到了Tomcat架构的一张图:

图解 & 深入浅出 JavaWeb:Servlet必会必知

二、Tomcat 简单说几句

如图,Tomcat 包含了核心服务模块:Connector连接模块 和 Container 容器。Tomcat Server 核心是一个 Servlet/JSP Container。对每一个HTTP请求,过程如下

如图:

图解 & 深入浅出 JavaWeb:Servlet必会必知

蓝色线指向过程是请求,绿色线指向过程是响应过程。也就是上面Web服务器核心过程:“连接过程 — 请求过程 — 应答过程 — 关闭连接

三、我第一个Servlet

什么是Servlet?(每次都会不停的问自己,这是什么“What”?紧接着应该是什么用“How”吧)

在 JavaEE 6文档中,介绍如下

所以,Servlet 是Web服务器核心工作的抽象。它不单单只是实现HttpServlet,可能实现有FtpServlet(这个我猜的)等。相对较多的Web开发,知道的肯定是HttpServlet。

在 JavaEE 6文档中,是这样介绍HttpServlet

光说不练假把式,练一个“Hello,Servlet/JSP World!”:

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /*
  9. * Copyright [2015] [Jeff Lee]
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. /**
  24. * @author Jeff Lee
  25. * @since 2015-6-25 19:46:45
  26. * HelloWrold案例
  27. */
  28. @WebServlet(urlPatterns = "/helloWorld.html")
  29. public class HelloWorldServletT extends HttpServlet{
  30. private static final long serialVersionUID = 1L;
  31. @Override
  32. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  33. throws ServletException, IOException{
  34. // 获取输出打印对象
  35. PrintWriter out = resp.getWriter();
  36. out.println("Hello,Servlet/JSP World!");
  37. }
  38. }
复制代码

右键该HelloWorldServletT.java文件 — Run As — Run On Server — 选择Tomcat服务器 — Finish即可

图解 & 深入浅出 JavaWeb:Servlet必会必知

图解 & 深入浅出 JavaWeb:Servlet必会必知

等待片刻,你可看到网页上如下输出。这就是客户端从HttpServlet获取到的响应:

图解 & 深入浅出 JavaWeb:Servlet必会必知

三、分析源码


  1. @WebServlet(urlPatterns = "/helloWorld.html")
复制代码

@WebServlet 注解用于声明一个HttpServlet的配置。其中,urlPatters = “/helloWorld.html”,urlPatterns复数形式,说明至少一个URL必须被申明。它和另一个value必须存在一个,但不能同时存在。如果要匹配多个URL路径的话,如下:

  1. @WebServlet(urlPatterns = { "/helloWorld01.html", "/helloWorld02.html" }
复制代码

下面有个@Override,重写了父类HttpServletdoGet方法。我们先看看父类HttpServlet。HttpServlet是一个抽象类,它提供了以下方法:

如图:

图解 & 深入浅出 JavaWeb:Servlet必会必知

对于不同请求,HttpServlet的子类必须相应的实现至少一个方法,通常来说,会是其中一个,这样代码比较清晰。那父类的doGet方法做了什么工作呢?

  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  2. throws ServletException, IOException
  3. {
  4. String protocol = req.getProtocol();
  5. String msg = lStrings.getString("http.method_get_not_supported");
  6. if (protocol.endsWith("1.1")) {
  7. resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
  8. } else {
  9. resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
  10. }
  11. }
复制代码

这里就简单的获取了下HTTP协议及Http Local信息,然后可以协议是否是1.1,做出分别是405或者400HTTP状态码的响应。

回到HelloWorldServletT.java 这里:

  1. @Override
  2. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  3. throws ServletException, IOException {
  4. // 获取输出打印对象
  5. PrintWriter out = resp.getWriter();
  6. out.println("Hello,Servlet/JSP World!");
  7. }
复制代码

表示该HelloWorldServletT会接受Http GET请求,并OOM到HttpServletRequest,并执行里面的逻辑代码和返回响应。 这里从HttpServletResponse对象中获取到输出打印对象PrintWriter,然后输出了“Hello,Servlet/JSP World!”。

完毕!哦还有一点补充补充补充:

五、深入Servlet 具体过程

又回到这个简单的 Get Servlet代码:

  1. public class HelloWorldServletT extends HttpServlet{
  2. private static final long serialVersionUID = 1L;
  3. @Override
  4. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  5. throws ServletException, IOException{
  6. // 获取输出打印对象
  7. PrintWriter out = resp.getWriter();
  8. out.println("Hello,Servlet/JSP World!");
  9. }
  10. }
复制代码

这过程总结如下:

过程图如下:

图解 & 深入浅出 JavaWeb:Servlet必会必知

蓝色线指向过程是请求,绿色线指向过程是响应过程,橙色线指向过程是内部处理过程。

有些面试题会这样问:

因此,Servlet对象实例化是在以第一次请求此Servlet时,如果访问后,实例对象存在内存中,只会在服务器停止时,它才会消失。它不会随着各个线程结束而结束。因此下次访问Servlet时,Servlet Container会搜索相应的Servlet,如果不存在Container新建相应的Servlet。这也是我们想要的结果。

再来个恶心的面试题:

六、小结

发现这一博客写的太多,回头一看。可以写成三个文章了。言归正传本文要点如下



最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 01:51

Copyright 2015-2025 djqfx

返回顶部