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.
我正在推出符合Servlet 2.5的J2EE代码,我想知道2.5和3之间的主要区别是什么。官方Sun文档和个人经历的指针是最受赞赏的。
If I shouldn't be concerning myself with 3 for the time being, just say so. Thanks!
如果我不应该暂时关注自己3,那就这么说吧。谢谢!
4 个解决方案
#1
133
UPDATE
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):
正如更新和更明确一样,这些是servlet 2.5和3之间的主要区别(我不是要详尽无遗,我只是提到最有趣的部分):
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 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>
In servlets 3, web.xml is optional and you can use annotations instead of XML. The same example:
在servlet 3中,web.xml是可选的,您可以使用注释而不是XML。同样的例子:
@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:
对于过滤器,您需要在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>
Equivalent using annotations in servlets 3:
使用servlet 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:
对于监听器(在本例中为ServletContextListener),在servlet 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.
- 在servlet 2.5中,只有一个单片web.xml文件。
- 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.
- 在servlet 3中,每个“可加载”jar都可以在其META-INF目录中有一个web-fragment.xml,用于指定servlet,过滤器等。这是为了允许库和框架指定自己的servlet或其他对象。
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()
在servlet 3中,ServletContextListener可以使用添加到SevletContext的以下方法添加动态servlet,过滤器和侦听器:addServlet(),addFilter()和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).
示例:假设某个servlet容器的线程池中有五个线程,并且每个请求都需要执行一个耗时的过程(如复杂的SQL查询)。
-
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()
(ordoGet()
,doPost()
, etc.) is executed from start to end and returns a response.使用servlet 2.5,如果servlet容器同时收到五个请求,并且五个可用线程开始执行该过程,那么这个servlet容器将耗尽可用线程,因为在service()(或doGet(),doPost之前线程不会返回))等从开始到结束执行并返回响应。
-
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 3.0,可以将此长时间进程委派给另一个线程并在发送响应之前完成service()(现在响应将由最新线程发送)。这样线程就可以*地接收新的响应。
An example of asynchronous support:
异步支持的一个示例:
Servlets 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
// ...
}
}
Servlets 3:
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.
接口AsyncContext还具有获取请求对象,响应对象和添加侦听器的方法,以便在进程完成时通知它们。
Programmatic login and logout (security enhancements)
In servlets 3, the interface HttpServletRequest
has been added two new methods: login(username, password)
and logout()
.
在servlet 3中,接口HttpServletRequest添加了两个新方法:login(用户名,密码)和logout()。
For more details, have a look at the Java EE 6 API.
有关更多详细信息,请查看Java EE 6 API。
#2
21
Servlet 3.0 has not yet been released, but it looks like it's very close. The most important changes in 3.0 are: Pluggability, Ease of development, Async Servlet, Security. Whether or not these are important to you is impossible for me to say.
Servlet 3.0尚未发布,但它看起来非常接近。 3.0中最重要的变化是:可插拔性,易于开发,异步Servlet,安全性。我不可能说这些对你来说是否重要。
The most significant of these is probably the support for asynchronous Servlets. Here's an article that describes this in detail. The full specification can be downloaded here.
其中最重要的可能是对异步Servlet的支持。这是一篇详细描述这一内容的文章。完整的规格可以在这里下载。
#3
13
As Don mentioned, the main areas of improvements and additions are:
正如Don所说,改进和增加的主要方面是:
- Pluggability (modularizing of web.xml)
- 可插拔性(web.xml的模块化)
- Ease of development (annotations, generics, convention over configuration)
- 易于开发(注释,泛型,约定优于配置)
- Async servlet support (for comet style programming, async web proxy, async web services)
- 异步servlet支持(用于彗星样式编程,异步Web代理,异步Web服务)
- Security enhancements (programmatic login/logout)
- 安全性增强(程序化登录/注销)
- Others (HttpOnly Cookie, Session tracking, EJB in WAR file)
- 其他(HttpOnly Cookie,会话跟踪,WAR文件中的EJB)
Check out the Javaone 2008 presentation "Java Servlet 3.0 API: What's new and exciting" for details.
有关详细信息,请查看Javaone 2008演示文稿“Java Servlet 3.0 API:有什么新内容和令人兴奋的内容”。
#4
1
This link will give enough info on Servlet 3
此链接将提供有关Servlet 3的足够信息
Servlet 3 supports annotation to eliminate web.xml
Servlet 3支持注释以消除web.xml
@WebServlet
@WebServletContextListener
@ServletFilter
@InitParam
#1
133
UPDATE
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):
正如更新和更明确一样,这些是servlet 2.5和3之间的主要区别(我不是要详尽无遗,我只是提到最有趣的部分):
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 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>
In servlets 3, web.xml is optional and you can use annotations instead of XML. The same example:
在servlet 3中,web.xml是可选的,您可以使用注释而不是XML。同样的例子:
@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:
对于过滤器,您需要在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>
Equivalent using annotations in servlets 3:
使用servlet 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:
对于监听器(在本例中为ServletContextListener),在servlet 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.
- 在servlet 2.5中,只有一个单片web.xml文件。
- 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.
- 在servlet 3中,每个“可加载”jar都可以在其META-INF目录中有一个web-fragment.xml,用于指定servlet,过滤器等。这是为了允许库和框架指定自己的servlet或其他对象。
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()
在servlet 3中,ServletContextListener可以使用添加到SevletContext的以下方法添加动态servlet,过滤器和侦听器:addServlet(),addFilter()和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).
示例:假设某个servlet容器的线程池中有五个线程,并且每个请求都需要执行一个耗时的过程(如复杂的SQL查询)。
-
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()
(ordoGet()
,doPost()
, etc.) is executed from start to end and returns a response.使用servlet 2.5,如果servlet容器同时收到五个请求,并且五个可用线程开始执行该过程,那么这个servlet容器将耗尽可用线程,因为在service()(或doGet(),doPost之前线程不会返回))等从开始到结束执行并返回响应。
-
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 3.0,可以将此长时间进程委派给另一个线程并在发送响应之前完成service()(现在响应将由最新线程发送)。这样线程就可以*地接收新的响应。
An example of asynchronous support:
异步支持的一个示例:
Servlets 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
// ...
}
}
Servlets 3:
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.
接口AsyncContext还具有获取请求对象,响应对象和添加侦听器的方法,以便在进程完成时通知它们。
Programmatic login and logout (security enhancements)
In servlets 3, the interface HttpServletRequest
has been added two new methods: login(username, password)
and logout()
.
在servlet 3中,接口HttpServletRequest添加了两个新方法:login(用户名,密码)和logout()。
For more details, have a look at the Java EE 6 API.
有关更多详细信息,请查看Java EE 6 API。
#2
21
Servlet 3.0 has not yet been released, but it looks like it's very close. The most important changes in 3.0 are: Pluggability, Ease of development, Async Servlet, Security. Whether or not these are important to you is impossible for me to say.
Servlet 3.0尚未发布,但它看起来非常接近。 3.0中最重要的变化是:可插拔性,易于开发,异步Servlet,安全性。我不可能说这些对你来说是否重要。
The most significant of these is probably the support for asynchronous Servlets. Here's an article that describes this in detail. The full specification can be downloaded here.
其中最重要的可能是对异步Servlet的支持。这是一篇详细描述这一内容的文章。完整的规格可以在这里下载。
#3
13
As Don mentioned, the main areas of improvements and additions are:
正如Don所说,改进和增加的主要方面是:
- Pluggability (modularizing of web.xml)
- 可插拔性(web.xml的模块化)
- Ease of development (annotations, generics, convention over configuration)
- 易于开发(注释,泛型,约定优于配置)
- Async servlet support (for comet style programming, async web proxy, async web services)
- 异步servlet支持(用于彗星样式编程,异步Web代理,异步Web服务)
- Security enhancements (programmatic login/logout)
- 安全性增强(程序化登录/注销)
- Others (HttpOnly Cookie, Session tracking, EJB in WAR file)
- 其他(HttpOnly Cookie,会话跟踪,WAR文件中的EJB)
Check out the Javaone 2008 presentation "Java Servlet 3.0 API: What's new and exciting" for details.
有关详细信息,请查看Javaone 2008演示文稿“Java Servlet 3.0 API:有什么新内容和令人兴奋的内容”。
#4
1
This link will give enough info on Servlet 3
此链接将提供有关Servlet 3的足够信息
Servlet 3 supports annotation to eliminate web.xml
Servlet 3支持注释以消除web.xml
@WebServlet
@WebServletContextListener
@ServletFilter
@InitParam