http://imyue.org/servlet-3-0/
今个看DWR的做测试的时候,下了Tomcat7,新建servlet的时候,发现Eclipse居然没有在web.xml中添加配置信息还能正常运行servlet,查找一番后才发现这是servlet3的新特性,增添了对annotation的支持,无须更改配置文件,只需添加注解即可。
其新特性如下:
- 异步处理支持:有了该特性,Servlet 线程不再需要一直阻塞,直到业务处理完毕才能再输出响应,最后才结束该 Servlet 线程。在接收到请求之后,Servlet 线程可以将耗时的操作委派给另一个线程来完成,自己在不生成响应的情况下返回至容器。针对业务处理较耗时的情况,这将大大减少服务器资源的占用,并且提高并发处理速度。
- 新增的注解支持:该版本新增了若干注解,用于简化 Servlet、过滤器(Filter)和监听器(Listener)的声明,这使得 web.xml 部署描述文件从该版本开始不再是必选的了。
- 可插性支持:熟悉 Struts2 的开发者一定会对其通过插件的方式与包括 Spring 在内的各种常用框架的整合特性记忆犹新。将相应的插件封装成 JAR 包并放在类路径下,Struts2 运行时便能自动加载这些插件。现在 Servlet 3.0 提供了类似的特性,开发者可以通过插件的方式很方便的扩充已有 Web 应用的功能,而不需要修改原有的应用。
对servlet创建的注解是@WebServlet
@WebServlet annotation 支持的属性如下:
- name – Name of the servlet – optional
- asyncSupported=true/false – Specifies weather the servlet supports asynchronous processing or not.
- largeIcon – large icon for this Servlet, if present – optional
- smallIcon – small icon for this Servlet, if present – optional
- description – Servlet description – optional
- initParams – Array of @WebInitParam, used to pass servlet config parameters – optional
- loadOnStartup – Integer value – optional
- displayName -Servlet display name – optional
- urlPatterns – Array of url patterns for which the sevlet should be invoked – Atlease one url pattern is requried
以下是一个实例.可以使用 /helloanno 来访问该servlet
01 | package org.servletworld.example; |
03 | import java.io.IOException; |
04 | import java.io.PrintWriter; |
06 | import javax.servlet.ServletException; |
07 | import javax.servlet.annotation.WebInitParam; |
08 | import javax.servlet.annotation.WebServlet; |
09 | import javax.servlet.http.HttpServlet; |
10 | import javax.servlet.http.HttpServletRequest; |
11 | import javax.servlet.http.HttpServletResponse; |
13 | @WebServlet (asyncSupported = false , name = "HelloAnnotationServlet" , urlPatterns = { "/helloanno" }, |
14 | initParams = { @WebInitParam (name= "param1" , value= "value1" ), @WebInitParam (name= "param2" , value= "value2" )} |
16 | public class HelloAnnotationServlet extends HttpServlet { |
19 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
20 | resp.setContentType( "text/html" ); |
21 | PrintWriter out = resp.getWriter(); |
22 | out.write( "<h2>Hello Friends! Welcome to the world of servlet annotation </h2>" ); |
24 | out.write( "<h4>I am a servlet 3, I have been defined using @WebServlet annotations.</h4>" ); |
26 | out.write( "The Init parameters passed to me are :" ); |
29 | out.write(li( "param1=" +getServletConfig().getInitParameter( "param1" )));; |
30 | out.write(li( "param2=" +getServletConfig().getInitParameter( "param2" )));; |
35 | private String li(String val) { |
36 | return "<li>" +val+ "</li>" ; |
这样就不用在配置文件中写以下代码了
01 | <?xml version= "1.0" encoding= "ISO-8859-1" ?> |
05 | <SERVLET-NAME>test</SERVLET-NAME> |
06 | <SERVLET-CLASS>test.test</SERVLET-CLASS> |
10 | <SERVLET-NAME>test</SERVLET-NAME> |
11 | <URL-PATTERN>/test/*</URL-PATTERN> |
14 | <SESSION-TIMEOUT> 30 </SESSION-TIMEOUT> |