Servlet 3.0

Servlet 3.0

http://imyue.org/servlet-3-0/

今个看DWR的做测试的时候,下了Tomcat7,新建servlet的时候,发现Eclipse居然没有在web.xml中添加配置信息还能正常运行servlet,查找一番后才发现这是servlet3的新特性,增添了对annotation的支持,无须更改配置文件,只需添加注解即可。

其新特性如下:

  1. 异步处理支持:有了该特性,Servlet 线程不再需要一直阻塞,直到业务处理完毕才能再输出响应,最后才结束该 Servlet 线程。在接收到请求之后,Servlet 线程可以将耗时的操作委派给另一个线程来完成,自己在不生成响应的情况下返回至容器。针对业务处理较耗时的情况,这将大大减少服务器资源的占用,并且提高并发处理速度。
  2. 新增的注解支持:该版本新增了若干注解,用于简化 Servlet、过滤器(Filter)和监听器(Listener)的声明,这使得 web.xml 部署描述文件从该版本开始不再是必选的了。
  3. 可插性支持:熟悉 Struts2 的开发者一定会对其通过插件的方式与包括 Spring 在内的各种常用框架的整合特性记忆犹新。将相应的插件封装成 JAR 包并放在类路径下,Struts2 运行时便能自动加载这些插件。现在 Servlet 3.0 提供了类似的特性,开发者可以通过插件的方式很方便的扩充已有 Web 应用的功能,而不需要修改原有的应用。

对servlet创建的注解是@WebServlet

@WebServlet annotation 支持的属性如下:

  1. name – Name of the servlet – optional
  2. asyncSupported=true/false – Specifies weather the servlet supports asynchronous processing or not.
  3. largeIcon – large icon for this Servlet, if present – optional
  4. smallIcon – small icon for this Servlet, if present – optional
  5. description – Servlet description – optional
  6. initParams – Array of @WebInitParam, used to pass servlet config parameters – optional
  7. loadOnStartup – Integer value – optional
  8. displayName -Servlet display name – optional
  9. urlPatterns – Array of url patterns for which the sevlet should be invoked – Atlease one url pattern is requried

以下是一个实例.可以使用 /helloanno 来访问该servlet

01package org.servletworld.example;
02  
03import java.io.IOException;
04import java.io.PrintWriter;
05  
06import javax.servlet.ServletException;
07import javax.servlet.annotation.WebInitParam;
08import javax.servlet.annotation.WebServlet;
09import javax.servlet.http.HttpServlet;
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12  
13@WebServlet(asyncSupported = false, name = "HelloAnnotationServlet", urlPatterns = {"/helloanno"},
14initParams = {@WebInitParam(name="param1", value="value1"), @WebInitParam(name="param2", value="value2")}
15)
16public class HelloAnnotationServlet extends HttpServlet {
17  
18    @Override
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>");
23        out.write("<br/>");
24        out.write("<h4>I am a servlet 3, I have been defined using @WebServlet annotations.</h4>");
25        out.write("<br/>");
26        out.write("The Init parameters passed to me are :");
27        out.write("<br/>");
28        out.write("<ol>");
29        out.write(li("param1="+getServletConfig().getInitParameter("param1")));;
30        out.write(li("param2="+getServletConfig().getInitParameter("param2")));;
31        out.write("</ol>");
32        out.close();
33    }
34  
35    private String li(String val) {
36        return "<li>"+val+"</li>";
37    }
38}

这样就不用在配置文件中写以下代码了

01<?xml version="1.0" encoding="ISO-8859-1"?>
02<WEB-APP>  
03  
04    <SERVLET>
05        <SERVLET-NAME>test</SERVLET-NAME>
06        <SERVLET-CLASS>test.test</SERVLET-CLASS>
07    </SERVLET>  
08  
09    <SERVLET-MAPPING>
10        <SERVLET-NAME>test</SERVLET-NAME>
11        <URL-PATTERN>/test/*</URL-PATTERN>
12    </SERVLET-MAPPING>
13    <SESSION-CONFIG>
14    <SESSION-TIMEOUT>30</SESSION-TIMEOUT>
15    </SESSION-CONFIG>
16</WEB-APP>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值