Servlet3.0新特性剖析

长平狐 发布于 2012/09/03 12:30
阅读 261
收藏 2
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) {
}
}




原文链接: http://blog.csdn.net/t12x3456/article/details/7654273
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部