Servlet 2.5 和 3 之间有什么区别? [英] What are the differences between Servlet 2.5 and 3?

查看:16
本文介绍了Servlet 2.5 和 3 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在滚动遵循 Servlet 2.5 的 J2EE 代码,我想知道 2.5 和 3 之间的主要区别是什么.最感谢提供 Sun 官方文档和个人经验的指针.

I'm rolling J2EE code that adheres to Servlet 2.5 and I'm wondering what are the major differences between 2.5 and 3. Pointers to official Sun docs and personal experiences are most appreciated.

如果我暂时不应该为自己担心 3,那就直说吧.谢谢!

If I shouldn't be concerning myself with 3 for the time being, just say so. Thanks!

推荐答案

UPDATE

作为更新,更明确地说,这些是 servlets 2.5 和 3 之间的主要区别(我不想详尽无遗,我只是提到最有趣的部分):

Just as an update and to be more explicit, these are the main differences between servlets 2.5 and 3 (I'm not trying to be exhaustive, I'm just mentioning the most interesting parts):

在 servlets 2.5 中,要声明带有一个 init 参数的 servlet,您需要将其添加到 web.xml:

In servlets 2.5, to declare a servlet with one init parameter you need to add this to web.xml:

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>my.server.side.stuff.MyAwesomeServlet</servlet-class>
    <init-param>
        <param-name>configFile</param-name>
        <param-value>config.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/path/to/my/servlet</url-pattern>
</servlet-mapping>

在 servlets 3 中,web.xml 是可选的,您可以使用注释代替 XML.同样的例子:

In servlets 3, web.xml is optional and you can use annotations instead of XML. The same example:

@WebServlet(name="myServlet",
    urlPatterns={"/path/to/my/servlet"},
    initParams={@InitParam(name="configFile", value="config.xml")})
public class MyAwesomeServlet extends HttpServlet { ... }

对于过滤器,您需要在 servlets 2.5 的 web.xml 中添加:

For filters, you need to add this in web.xml in servlets 2.5:

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>my.server.side.stuff.MyAwesomeServlet</filter-class>
</filter>
<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/path/to/my/filter</url-pattern>
</filter-mapping>

相当于在 servlets 3 中使用注解:

Equivalent using annotations in servlets 3:

@ServletFilter(name="myFilter", urlPatterns={"/path/to/my/filter"})
public class MyAwesomeFilter implements Filter { ... }

对于侦听器(在本例中为 ServletContextListener),在 servlets 2.5 中:

For a listener (in this case a ServletContextListener), in servlets 2.5:

<listener>
    <listener-class>my.server.side.stuff.MyAwesomeListener</listener-class>
</listener>

同样使用注解:

@WebServletContextListener
public class MyAwesomeListener implements ServletContextListener { ... }

web.xml 的模块化(可插拔性)

  • 在 servlets 2.5 中只有一个单一的 web.xml 文件.
  • 在 servlets 3 中,每个可加载"jar 在其 META-INF 目录中都有一个 web-fragment.xml,用于指定 servlets、过滤器等.这是为了允许库和框架指定自己的 servlet 或其他对象.
  • Modularization of web.xml (Pluggability)

    • In servlets 2.5 there is just one monolithic web.xml file.
    • In servlets 3, each "loadable" jar can have a web-fragment.xml in its META-INF directory specifying servlets, filters, etc. This is to allow libraries and frameworks to specify their own servlets or other objects.
    • 在 servlets 3 中,ServletContextListener 可以使用添加到 SevletContext 的以下方法动态添加 servlets、过滤器和侦听器:addServlet()addFilter()addListener()

      In servlets 3, a ServletContextListener can add dynamically servlets, filters and listeners using the following methods added to SevletContext: addServlet(), addFilter() and addListener()

      例如:假设某个 servlet 容器在其线程池中有五个线程,并且每个请求都需要执行一个耗时的过程(如复杂的 SQL 查询).

      Example: say that some servlet container has five threads in its thread pool, and there is a time-consuming process to be executed per request (like a complex SQL query).

      • 在 servlets 2.5 中,如果这个 servlet 容器同时接收五个请求并且五个可用线程开始执行该过程,则该 servlet 容器将耗尽可用线程,因为线程直到 service()(或doGet()doPost()等)从头到尾执行并返回响应.

      • With servlets 2.5 this servlet container would run out of available threads if it receives five requests at the same time and the five available threads start doing the process, because the threads wouldn't return until service() (or doGet(), doPost(), etc.) is executed from start to end and returns a response.

      使用 servlets 3.0,这个长时间的进程可以委托给另一个线程并在发送响应之前完成service()(现在响应将由最新的线程发送).这样线程就可以自由地接收新的响应.

      With servlets 3.0, this long-time process can be delegated to another thread and finish service() before sending the response (the response now will be sent by the latest thread). This way the thread is free to receive new responses.

      异步支持的一个例子:

      Servlet 2.5:

      Servlets 2.5:

      public class MyAwesomeServlet extends HttpSerlvet {
      
          @Override
          public void doGet(HttpServletRequest request, HttpServletResponse response) {
              // ...
      
              runSlowProcess();
              // no async support, thread will be free when runSlowProcess() and
              // doGet finish
      
              // ...
          }
      
      }
      

      Servlet 3:

      @WebServlet(name="myServlet",
                   urlPatterns={"/mySlowProcess"},
                   asyncSupported=true) // asyncSupported MUST be specified for
                                        // servlets that support asynchronous
                                        // processing
      public class MyAwesomeServlet extends HttpSerlvet {
      
          @Override
          public void doGet(HttpServletRequest request, HttpServletResponse response) {
      
      
              // an AsyncContext is created, now the response will be completed
              // not when doGet finalizes its execution, but when
              // myAsyncContext.complete() is called.
              AsyncContext myAsyncContext = request.startAsync(request, response);
      
              // ...
      
              // myAsyncContext is passed to another thread
              delegateExecutionToProcessingThread(myAsyncContext);
      
              // done, now this thread is free to serve another request
          }
      
      }
      
      // ... and somewhere in another part of the code:
      
      public class MyProcessingObject {
      
          public void doSlowProcess() {
      
              // ...
      
              runSlowProcess();
              myAsyncContext.complete(); // request is now completed.
      
              // ...
      
          }
      
      }
      

      接口 AsyncContext 也有方法来获取请求对象、响应对象并添加监听器以在进程完成时通知它们.

      The interface AsyncContext also has methods to get the request object, response object and add listeners to notify them when a process has finished.

      在 servlets 3 中,接口 HttpServletRequest 已添加两个新方法:login(username, password)logout().

      In servlets 3, the interface HttpServletRequest has been added two new methods: login(username, password) and logout().

      有关更多详细信息,请查看 Java EE 6 API.

      For more details, have a look at the Java EE 6 API.

      这篇关于Servlet 2.5 和 3 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆