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

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

问题描述

我正在推出符合Servlet 2.5的J2EE代码,我想知道2.5和3之间的主要区别是什么。非常感谢官方Sun文档和个人体验的指针。



如果我不应该暂时关注自己3,那就这么说吧。谢谢!

解决方案

更新



就像更新一样更明确的是,这些是servlet 2.5和3之间的主要区别(我不是要详尽无遗,我只是提到最有趣的部分):



注释声明servlet,过滤器和监听器(易于开发)



在servlet 2.5中,要使用一个init参数声明一个servlet,需要将其添加到 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>

在servlet 3中, web.xml 是可选的,您可以使用注释而不是XML。相同的例子:

  @WebServlet(name =myServlet,
urlPatterns = {/ path / to / my / servlet},
initParams = {@ InitParam(name =configFile,value =config.xml)})
公共类MyAwesomeServlet扩展HttpServlet {...}

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

 < 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>

等效使用servlet 3中的注释:

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

对于监听器(在本例中为ServletContextListener),在servlet 2.5中:

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

使用注释相同:

  @WebServletContextListener 
公共类MyAwesomeListener实现ServletContextListener {...}



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




  • 在servlet 2.5中只有一个单片 web.xml file。

  • 在servlet 3中,每个可加载jar可以在其 META-INF 中包含 web-fragment.xml 指定servlet,过滤器等的目录。这是为了允许库和框架指定自己的servlet或其他对象。



动态注册上下文初始化时的servlet,过滤器和监听器(Pluggability)



在servlet 3中, ServletContextListener 可以添加动态servlet ,使用以下方法添加到 SevletContext :过滤器和侦听器: addServlet() addFilt er() addListener()



异步支持



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




  • 对于servlet 2.5,如果同时收到五个请求并且五个可用线程开始执行该过程,则该servlet容器将耗尽可用线程,因为线程不会返回 service()(或 doGet() doPost()等)从开始到结束执行并返回响应。


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




异步支持的一个例子:



Servlets 2.5:

 公共类MyAwesomeServlet扩展HttpSerlvet {

@Override
public void doGet(HttpServletRequest request,HttpServletResponse response){
// ... ...

runSlowProcess();
//没有异步支持,当runSlowProcess()和
// doGet完成

// ...
}
$时,线程将是免费的b $ b}

Servlets 3:

  @WebServlet(name =myServlet,
urlPatterns = {/ mySlowProcess},
asyncSupported = true)//必须为$ b $指定asyncSupported b //支持异步的servlet
//处理
公共类MyAwesomeServlet扩展HttpSerlvet {

@Override
public void doGet(HttpServletRequest request,HttpServletResponse response){


//创建了一个AsyncContext,现在响应将完成
//不是在doGet完成执行时,而是在
// myAsyncContext.complete()时叫做。
AsyncContext myAsyncContext = request.startAsync(request,response);

// ...

// myAsyncContext传递给另一个线程
delegateExecutionToProcessingThread(myAsyncContext);

//完成,现在这个帖子可以免费提供另一个请求
}

}

// ...等等在代码的另一部分:

public class MyProcessingObject {

public void doSlowProcess(){

// ... ...

runSlowProcess();
myAsyncContext.complete(); //请求现已完成。

// ...

}

}

接口 AsyncContext 还有获取请求对象,响应对象和添加侦听器的方法,以便在进程完成时通知它们。 / p>

程序化登录和注销(安全性增强)



在servlets 3中,接口 HttpServletRequest 已添加两个新方法:登录(用户名,密码) logout()



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


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.

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

解决方案

UPDATE

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):

Annotations to declare servlets, filters and listeners (ease of development)

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>

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 { ... }

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>

Equivalent using annotations in servlets 3:

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

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

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

The same using annotations:

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

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.

Dynamic registration of servlets, filters and listeners at context initialization time (Pluggability)

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

Asynchronous support

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).

  • 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.

  • 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.

An example of asynchronous support:

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

        // ...
    }

}

Servlets 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.

        // ...

    }

}

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

Programmatic login and logout (security enhancements)

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

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

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

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