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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
default-lazy-init="true">
<!--启用注解 定义组件查找规则 -->
<context:component-scan base-package="com.demo">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- servlet适配器,这里必须明确声明,因为spring默认没有初始化该适配器 -->
<bean id="servletHandlerAdapter" class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter"/>
<!-- demo servlet -->
<bean name="/demo.do" class="com.demo.DemoServlet"/>
</beans>
此时web.xml中无需声明servlet配置。
DemoServlet:
package com.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
@SuppressWarnings("serial")
public class DemoServlet extends HttpServlet{
private static final String CONTENT_TYPE = "application/xml; charset=UTF-8";
@Autowired
private DemoService service;
/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType(CONTENT_TYPE);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String result = service.testDemo();
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<root>\n");
out.println("<result>"+result+"</result>\n");
out.println("</root>\n");
out.flush();
out.close();
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
DemoService:
package com.demo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
private static Logger logger = Logger.getLogger(DemoService.class);
/**
* 描述 : <描述函数实现的功能>. <br>
*<p>
* @return
*/
public String testDemo(){
logger.info("demo");
return "test";
}
}
启动服务器,请求http://localhost:8080/servlet/demo.do
输出结果:
<?xml version="1.0" encoding="UTF-8" ?>