spring管理servlet的利器--SimpleServletHandlerAdapter

servlet现在有许多项目仍在使用,一般作为接口调用,很早以前我曾发表过一篇关于《Spring管理Filter和Servlet 》的文章,后来发现spring提供了更为简单的管理servlet的方式,那就是使用SimpleServletHandlerAdapter,基于这个适配器,可以将servlet像普通bean一样声明到spring配置文件中,而无需在web.xml中声明。

 

先来说一下SimpleServletHandlerAdapter,它是spring提供的处理适配器,专门适配类型为javax.servlet.Servlet的处理器,spring提供了多种适配器,分别负责不同的处理器适配,比如我们现在很常用的基于注解的MVC,就是使用的AnnotationMethodHandlerAdapter这个适配器。

 

不过这里要说明的是,spring的默认策略中并不包含SimpleServletHandlerAdapter,所以在使用时需要明确声明,另外,如果还用了其它的处理器,也要明确声明那个适配器,因为默认配置已经被替换了,比如用了springMVC注解,就需要明确声明AnnotationMethodHandlerAdapter这个适配器。

 

一个简单的基于注解注入的配置文件如下:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:ehcache="http://www.springmodules.org/schema/ehcache"  
  6.     xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.             http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"  
  10.             default-lazy-init="true">  
  11.   
  12.   
  13.     <!--启用注解   定义组件查找规则 -->  
  14.     <context:component-scan base-package="com.demo">  
  15.         <context:include-filter type="annotation"  
  16.             expression="org.springframework.stereotype.Service" />  
  17.     </context:component-scan>  
  18.   
  19.     <!-- servlet适配器,这里必须明确声明,因为spring默认没有初始化该适配器 -->  
  20.     <bean id="servletHandlerAdapter" class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter"/>  
  21.   
  22.    
  23.     <!-- demo servlet -->  
  24.     <bean name="/demo.do" class="com.demo.DemoServlet"/>  
  25.   
  26.    
  27. </beans>  

  

 

此时web.xml中无需声明servlet配置。

  

DemoServlet:

 

Java代码   收藏代码
  1. package com.demo;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. @SuppressWarnings("serial")  
  14. public class DemoServlet extends HttpServlet{  
  15.   
  16.     private static final String CONTENT_TYPE = "application/xml; charset=UTF-8";  
  17.   
  18.     @Autowired  
  19.     private DemoService service;  
  20.   
  21.     /* (non-Javadoc) 
  22.      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 
  23.      */  
  24.     @Override  
  25.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  26.             throws ServletException, IOException {  
  27.         // TODO Auto-generated method stub  
  28.         response.setContentType(CONTENT_TYPE);  
  29.         response.setHeader("Pragma", "No-cache");  
  30.         response.setHeader("Cache-Control", "no-cache");  
  31.         response.setDateHeader("Expires", 0);  
  32.         String result = service.testDemo();  
  33.         PrintWriter out = response.getWriter();  
  34.         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");  
  35.         out.println("<root>\n");  
  36.         out.println("<result>"+result+"</result>\n");  
  37.         out.println("</root>\n");  
  38.           
  39.         out.flush();  
  40.         out.close();  
  41.     }  
  42.   
  43.     /* (non-Javadoc) 
  44.      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 
  45.      */  
  46.     @Override  
  47.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  48.             throws ServletException, IOException {  
  49.         // TODO Auto-generated method stub  
  50.         doGet(request, response);  
  51.     }  
  52.   
  53.       
  54.       
  55. }  

 

DemoService :

 

Java代码   收藏代码
  1. package com.demo;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.stereotype.Service;  
  5. @Service  
  6. public class DemoService {  
  7.       
  8.     private static Logger logger = Logger.getLogger(DemoService.class);  
  9.       
  10.     /**                                                           
  11.     * 描述 : <描述函数实现的功能>. <br> 
  12.     *<p>                                                                                                                                                                                                                                                        
  13.     * @return                                                                                                   
  14.     */  
  15.     public String testDemo(){  
  16.         logger.info("demo");  
  17.         return "test";  
  18.     }  
  19. }  

  

启动服务器,请求http://localhost:8080/servlet/demo.do

输出结果:

<?xml version="1.0" encoding="UTF-8" ?>

< root >
   < result > test </ result >
</ root >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值