Java Servlet 2.5 和 3 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1638865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between Servlet 2.5 and 3?
提问by Max A.
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,那就直说吧。谢谢!
采纳答案by morgano
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)
用于声明 servlet、过滤器和侦听器的注释(易于开发)
In servlets 2.5, to declare a servlet with one init parameter you need to add this to web.xml:
在 servlets 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.xmlis optional and you can use annotations instead of XML. The same example:
在 servlets 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.xmlin servlets 2.5:
对于过滤器,您需要在 servlets 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),在 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)
web.xml 的模块化(Pluggability)
- In servlets 2.5 there is just one monolithic web.xmlfile.
- In servlets 3, each "loadable" jar can have a web-fragment.xmlin its META-INFdirectory specifying servlets, filters, etc. This is to allow libraries and frameworks to specify their own servlets or other objects.
- 在 servlets 2.5 中只有一个单一的web.xml文件。
- 在 servlet 3 中,每个“可加载”jar在其META-INF目录中都有一个web-fragment.xml,用于指定 servlet、过滤器等。这是为了允许库和框架指定它们自己的 servlet 或其他对象。
Dynamic registration of servlets, filters and listeners at context initialization time (Pluggability)
在上下文初始化时动态注册 servlet、过滤器和侦听器(Pluggability)
In servlets 3, a ServletContextListener
can add dynamically servlets, filters and listeners using the following methods added to SevletContext
: addServlet()
, addFilter()
and addListener()
在小服务程序3中,ServletContextListener
可以动态地添加加入到小服务程序,过滤器和使用以下方法听众SevletContext
: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.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这个servlet容器将耗尽可用线程,如果它收到与此同时五个请求和五个可用线程开始做的过程中,因为线程不会返回,直到
service()
(或者doGet()
,doPost()
等)从开始执行结束并返回响应。使用 servlets 3.0,这个长时间的进程可以委托给另一个线程并
service()
在发送响应之前完成(现在响应将由最新的线程发送)。这样线程就可以自由地接收新的响应。
An example of asynchronous support:
异步支持的一个例子:
Servlets 2.5:
小服务程序 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:
小服务程序 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()
.
在 servlets 3 中,该接口HttpServletRequest
添加了两个新方法:login(username, password)
和logout()
。
For more details, have a look at the Java EE 6 API.
有关更多详细信息,请查看Java EE 6 API。
回答by Dónal
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 articlethat describes this in detail. The full specification can be downloaded here.
回答by Pascal Thivent
As Don mentioned, the main areas of improvements and additions are:
正如 Don 所提到的,改进和添加的主要领域是:
- Pluggability (modularizing of web.xml)
- Ease of development (annotations, generics, convention over configuration)
- Async servlet support (for comet style programming, async web proxy, async web services)
- Security enhancements (programmatic login/logout)
- Others (HttpOnly Cookie, Session tracking, EJB in WAR file)
- 可插入性(web.xml 的模块化)
- 易于开发(注释、泛型、约定优于配置)
- 异步 servlet 支持(用于 Comet 风格编程、异步 Web 代理、异步 Web 服务)
- 安全增强(程序化登录/注销)
- 其他(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:新增功能和令人兴奋的内容”。
回答by Ravi Parekh
This link will give enough info on Servlet 3
Servlet 3 supports annotation to eliminate web.xml
Servlet 3 支持注解消除 web.xml
@WebServlet
@WebServletContextListener
@ServletFilter
@InitParam