java 基础 --- servlet

Posted float123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 的交互。

 

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:

  1. Tomcat receives a request from a client through one of its connectors.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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的主要内容,如果未能解决你的问题,请参考以下文章

jsp基础语言-jsp代码段

java基础 第十四章(Servlet声明周期Servlet向jsp中提供数据Servlet跳转jspjsp中书写java代码)

JSP基础--JSP入门

Servlet基础

Java web中使用到的技术

java web学习: 三大组件之servlet基础