Servlet & JSP学习笔记之简介

前言

对于Java程序而言:

  • JVM >> 操作系统
  • class文件 >> 可执行文件

Web容器

Web容器可以抽象为运行Sevrlet&JSP的服务器,这个服务器是跑在JVM之上的。
Web容器位于HTTP服务器的上层,在HTTP协议以及JAVA对象中相互转化。

Servlet & JSP

JSP > .java源文件 > .class文件

Servlet运行原理

Servlet运行主要经过三个过程:

1. 创建Servlet对象

Web容器将HTTP报文转换成Servlet对象

image.png

2. 调用相关方法

image.png

Servlet主要是通过service方法来响应HTTP请求, 通过判断请求的Method来调用相应的响应函数. 响应函数在这个抽象类中并没有具体的实现, 需要子类继承来实现.

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
# get 方法需要判断资源是否有缓存, 而且为了兼容旧版本协议做了两次判断
        if(method.equals("GET")) {
            lastModified = this.getLastModified(req);
            if(lastModified == -1L) {
                this.doGet(req, resp);
            } 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")) {
            lastModified = this.getLastModified(req);
            this.maybeSetLastModified(resp, lastModified);
            this.doHead(req, resp);
        } else if(method.equals("POST")) {
            this.doPost(req, resp);
        } else if(method.equals("PUT")) {
            this.doPut(req, resp);
        } else if(method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if(method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if(method.equals("TRACE")) {
            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);
        }

    }

3. 销毁对象

将Servlet对象转化为HTTP报文,并销毁该对象

image.png

URL Mapping

在Servlet3.0之后可以使用注解的方式配置URL, 而不用配置XML文件.

@WebServlet("/hello.view")
public class HelloServlet extends javax.servlet.http.HttpServlet {
    public HelloServlet() {
    }

或者可以使用web.xml 配置文件


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>helloServlet</servlet-name>
        <servlet-class>cn.boolin.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet</servlet-name>
        <url-pattern>/test1</url-pattern>
    </servlet-mapping>
</web-app>

文件结构

image.png

参考书目:

  • 林信良 (2012-05-01). JSP & Servlet学习笔记(第2版) (Kindle Location 535). 清华大学出版社. Kindle Edition.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自陈明乾的博客,可能有一定更新。 转原文声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、...
    C86guli阅读 10,147评论 6 72
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,429评论 11 349
  • 0 系列目录# WEB请求处理 WEB请求处理一:浏览器请求发起处理 WEB请求处理二:Nginx请求反向代理 本...
    七寸知架构阅读 14,748评论 22 190
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,899评论 18 399
  • 电子行业进入寒冬时期,我们公司也不另外,每个家里都有囤货,扛不住的开始低价抛,现金才是王道,有单的客户是大爷,个个...
    甜心教主阅读 1,059评论 0 0