`

读Servlet规范小记

阅读更多

1.Containers 又名 Servlet Engines, 是对 Web 服务器的扩展,提供了 servlet functionality.

 

2.Servlet Containers   web server application server 的部分,它负责发送 requests/responses ,decodes MIME-based requests, format MIM-based responses ,同时管理 Servlet Filter 的生命周期。

 

3.Servlet :

    由容器根据其配置来调用, Servlet 只负责处理 requests/responses ,根据请求输出响应。

    Servlet Mapping 的匹配顺序或优先级如下:

    1 t he exact match 精确匹配(完全一样)

              除了下面红色 字体列出的情况外的,均认为是精确匹配。

    2 the longest path match 最长 路径匹配

             所有以“ / 开始 以“ /* 结束 的用做路径匹配,匹配时选择最长的

    3 the extension match 扩展匹配

       所有以 .* 开始 的,用于扩展匹配

    4 )如果前三种都不匹配,则容器将其作为 resource 访问处理。如果定义了默认的 Servlet ,则采用默认的             

           Servlet 处理请求。

              仅包含“ / 字符串的,用于默认 Servlet 匹配

    5 )所有匹配都大小写敏感。

   


4.Servlet 3.0 引进了处理异步请求的机制

 

5.Request:

    每个 request 对象只在 servlet service 方法或 filter doFilter 方法内有效(处理异步请求除外)。

 

6. requestURI = contextPath + servletPath + pathInfo   servletPath 根据Servlet Mapping获得。 Context Path 对应一个 web 应用,如每个 war 包就定义了一个 Context path. 每个 path 如果不为空则以‘ / ’开始,而不以‘ / ’结束。

     

7.Servlet Context : 定义了 Servlet 可以看到的路径或者参数等,与 Web 应用一一对应。

    The ServletContext interface defines a servlet’s view of the Web application within

which the servlet is running.

Using the

    ServletContext object, a servlet can log events, obtain URL references to resources,

and set and store attributes that other servlets in the context(同一 Context ) can access.

    A ServletContext is rooted at a known path within a Web server. For example, a

servlet context could be located at http://www.mycorp.com/catalog . All requests

that begin with the /catalog request path, known as the context path, are routed to

the Web application associated with the ServletContext.

每个 Web 应用只有一个ServletContext ,它是单例的 。它提供了配置方法可供调用,但所有的配置方法只在contextInitialized事件发生时有效,其它地方调用这些方法都是非法的。

 

   ServletContext 提供了对其路径下所有资源的访问(静态)对资源的访问通过调用getResource 或getResourceAsStream。如getResource("/index.jsp")只返回 index.jsp 的代码,不会对其处理。

The full listing of the resources in the Web application can be accessed using the

getResourcePaths(String path) method.

 

Servlet contexts can   not be shared across virtual hosts.

Servlet contexts 临时工作目录可以通过javax.servlet.context.tempdir获得。

 

8.Response:

    对象封装了所有要返回客户端的信息,出于有效性,一般 Container 会缓存到 client 的输出。

9.Filter

    官方定义:A filter is a reusable piece of code that can transform the content of HTTP requests,responses, and header information.

    Filter 通常不会创建 Response 或像 Servlet 一样处理响应请求,他们只对请求或响应做适当的修改( they modify or adapt the requests for a   resource, and modify or adapt responses from a resource).

     

    Examples of Filtering Components

    ■ Authentication filters

    ■ Logging and auditing filters

    ■ Image conversion filters

    ■ Data compression filters

    ■ Encryption filters

    ■ Tokenizing filters

    ■ Filters that trigger resource access events

    ■ XSL/T filters that transform XML content

    ■ MIME-type chain filters

    ■ Caching filters

     

    Filter Chain 可以调用 Filter servlet ,最后调用的肯定是 Servlet JSP

    整个FilterChain的调用(包括了对Servlet的调用)可以想象成是堆栈方式的,每doFilter() 一次就压栈一次,而栈顶肯定是Servlet或JSP,Servlet将响应输出,然后就是每个Filter出栈,做一些其他的事情。

    Servlet 的响应方式不会改变,它按传进的Re sponse 响应。 Servlet 的输出直接发往客户端。而每个 Filter doFilter() 方法后,再处理 Response 是没有用的,只是用来做 些扫尾工作,比如释放引用的资源。

     

    do Filter 方法的代码: servlet 3.0 规范对 doFilter 的实现模式进行了描述,供分为 8 步,开发者可以根据需要只选择其中的部分步骤。

     

    Public void doFilter(request,resp0nse,chain){

        // 处理 request ,可以对 request 处理 ,response 做包装等,后续的 filter servlet jsp 得到的是包装或处理

        // 后的。

        …...

        chain .doFilter(request,reponse)

        // 扫尾工作

        …..

    }

     

     

     

    Filter 也可以处理 request-dispatched:

     

10.Session

    跟踪客户端信息:

       1 Coo kies

   2) URL Rewriting

   3) SSL Sessions

 

11. Dispatching Requests

When building a Web application, it is often useful to forward processing of a

request to another servlet, or to include the output of another servlet in the response.

RequestDispatcher 通过调用 ServletC ontext.getRequestDispatcher(path)获得。

The RequestDispatcher interface provides a mechanism to accomplish this.

To use a request dispatcher, a servlet calls either the include method or forward

method of the RequestDispatcher interface.

 

 

12. The application events

The application events facility gives the Web Application Developer greater control

over the lifecycle of the ServletContext and HttpSession and ServletRequest,

allows for better code factorization, and increases efficiency in managing the

resources that the Web application uses.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics