Servlet3.0规范的新特性主要是为了3个目的:
1.简化开发
2.便于布署
3.支持Web2.0原则
为了简化开发流程,Servlet3.0引入了注解(annotation),这使得web布署描述符web.xml不在是必须的选择。
Pluggability可插入性
当使用任何第三方的框架,如Struts,JSF或Spring,我们都需要在web.xml中添加对应的Servlet的入口。这使得web描述符笨重而难以维护。Servlet3.0的新的可插入特性使得web应用程序模块化而易于维护。通过webfragment实现的可插入性减轻了开发人员的负担,不需要再在web.xml中配置很多的Servlet入口。
Asynchronous Processing 异步处理
另外一个显著的改变就是Servlet3.0支持异步处理,这对AJAX应用程序非常有用。当一个Servlet创建一个线程来创建某些请求的时候,如查询数据库或消息连接,这个线程要等待直到获得所需要的资源才能够执行其他的操作。异步处理通过运行线程执行其他的操作来避免了这种阻塞。
Apart from the features mentioned here, several other enhancementshave been made to the existing API. The sections towards the end ofthe article will explore these features one by one in detail.
除了这些新特性之外, Servlet3.0对已有的API也做了一些改进,在本文的最后我们会做介绍。
Annotations in Servlet Servlet中使用注解
Servlet3.0的一个主要的改变就是支持注解。使用注解来定义Servlet和filter使得我们不用在web.xml中定义相应的入口。
@WebServlet
@WebServlet用来定义web应用程序中的一个Servlet。这个注解可以应用于继承了HttpServlet。这个注解有多个属性,例如name,urlPattern,initParams,我们可以使用者的属性来定义Servlet的行为。urlPattern属性是必须指定的。
例如我们可以象下面的例子这样定义:
@WebServlet(name = "GetQuoteServlet", urlPatterns = {"/getquote"})
public class GetQuoteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String symbol = request.getParameter("symbol");
out.println("<h1>Stock Priceis</h1>" +StockQuoteBean.getPrice(symbol);
} finally {
out.close();
}
}
}
public class StockQuoteBean {
private StockQuoteServiceEntity serviceEntity = newStockQuoteServiceEntity();
public double getPrice(String symbol) {
if(symbol !=null ) {
return serviceEntity.getPrice(symbol);
} else {
return 0.0;
}
}
}
在上面的例子中,一个Servlet只对应了一个urlPattern。实际上一个Servlet可以对应多个urlPattern,我们可以这样定义:
@WebServlet(name = "GetQuoteServlet", urlPatterns = {"/getquote","/stockquote"} )
public class GetQuoteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String symbol = request.getParameter("symbol");
out.println("<h1>Stock Priceis</h1>" +StockQuoteBean.getPrice(symbol);
} finally {
out.close();
}
}
}
@WebFilter
我们可以使用@WebFilter注解来定义filter。这个注解可以被应用在实现了javax.servlet.Filter接口的类上。同样的,urlPattern属性是必须指定的。下面就是一个例子。
@WebFilter(filterName = "AuthenticateFilter", urlPatterns ={"/stock.jsp", "/getquote"})
public class AuthenticateFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponseresponse,
FilterChain chain) throws IOException, ServletException {
String username = ((HttpServletRequest)request).getParameter("uname");
String password = ((HttpServletRequest)request).getParameter("password");
if (username == null || password == null) {
((HttpServletResponse) response).sendRedirect("index.jsp"); }
if (username.equals("admin") && password.equals("admin")){
chain.doFilter(request, response); }
else {
((HttpServletResponse) response).sendRedirect("index.jsp"); }
}
public void destroy() {
}
public void init(FilterConfig filterConfig) {
}
}
分享到:
相关推荐
Servlet3.0新特性解析_03_Servlet3.0监听器、过滤器实现方式详解及Servlet的动态注册剖析
本课程针对有一定程序开发基础并有志于JAVA WEB开发的学员,理解MVC开发模式,能够熟练使用PowerDesigner 设计数据库,熟练使用mybatis进行开发,熟悉Servlet3.0的一些新特性,掌握bootstrap基本用法 课程大纲 ...
《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》内容全面,涵盖了从事Java Web开发所应掌握的所有知识,同时还讲解了最新的Servlet 3.0和JSP 2.2规范新增的特性。在知识的...
1.3 JDBC3.0 API的新特点 1.3.1 JDBC 3.0 API的一致性 1.3.2 不赞成的API 1.4 JDBC 3.0中的类和接口 1.4.1 java.sql包中的类和接口及其使用 1.4.2 javax.sql包中所含内容及其使用 1.5 JDBC驱动程序简介 1.5.1...
UltraEdit 7.2 WebGain Studio 4 with VisualCafe Forte for Java开发指南 Dreamweaver UltraDev IBM NetObjects ScriptBuilder3.0 使用HomeSite加速JSP的开发 Java2的安全新特性下的Applet...
172 10.5 小结 177 第11章 EJB 179 11.1 EJB简介 179 11.1.1 EJB的特点 179 11.1.2 EJB类型与组成 180 11.1.3 EJB 3.0的新特性 181 11.2 元数据注释和部署描述符 181 11.2.1 元数据注释 181 11.2.2 部署描述符 182 ...
附录b j2se 5.0的新特性与性能的提升 附录c 编排代码的精美工具jxbeauty 使用有道云笔记,轻松同步、管理您各终端的所有笔记。三重备份,存储数据安全有保障。免费的超大存储空间,无限量增长。激活后即可免费获得2...
SQL Server 2000与以前版本相比较,又具有以下新特性 : 1.支持XML(Extensive Markup Language,扩展标记语言) 2.强大的基于Web的分析 3.支持OLE DB和多种查询 4.支持分布式的分区视图 安装、...
第一章 Java5.0新特性.......................94 第二章 基础知识.....96 第三章 泛型...........101 第四章 Annotation.102 XML学习笔记...............106 XML前言...............106 第一章 XML Overview..........
第一章 Java5.0新特性.......................94 第二章 基础知识.....96 第三章 泛型...........101 第四章 Annotation.102 XML学习笔记...............106 XML前言...............106 JavaEE@xuxiang 2 Java/...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
INP:Oracle 3.0版或早期版本的表单源代码 INRS:INRS远程通信声频 INS:InstallShield安装脚本;X-Internet签字文件;Ensoniq EPS字簇设备;Cell/ⅡMAC/PC抽样设备 INT:中间代码,当一个源程序经过语法检查后...