Spring+Servlet

[java]  view plain copy print ?
  1. 在实际开发中我们需要用到Spring和Servlet这样的组合(比如写一个小项目啥的)下面我来整理一下我我的整合:  

第一步:在web.xml中注册Spring的监听器

这时我们在Web项目中添加Spring的第一步(当然肯定要添加Spring所需要的Jar包啦)

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <!-- Spring -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>  
  12.             /WEB-INF/classes/applicationContext.xml  
  13.         </param-value>  
  14.     </context-param>  
  15.     <listener>  
  16.         <listener-class>  
  17.             org.springframework.web.context.ContextLoaderListener  
  18.         </listener-class>  
  19.     </listener>  
  20.    
  21.   </servlet-mapping>  
  22.       
  23.    <welcome-file-list>  
  24.      <welcome-file>index.jsp</welcome-file>  
  25.    </welcome-file-list>  
  26. </web-app></span>  

第二步:编写一个管理Servlet的类

这个类主要是将Servlet和Spring中的Bean结合起来,方便Spring对Servlet进行管理,起到一个中转的作用

[java]  view plain copy print ?
  1. package com.xwl.estore.servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.GenericServlet;  
  6. import javax.servlet.Servlet;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10.   
  11. import org.springframework.web.context.WebApplicationContext;  
  12. import org.springframework.web.context.support.WebApplicationContextUtils;  
  13.   
  14. @SuppressWarnings("serial")  
  15. /** 
  16.  * 我们自己实现的一个代理类用于将Servlet转为Spring管理的Servlet Bean 
  17.  */  
  18. public class ServletToBeanProxy extends GenericServlet {  
  19.   
  20.     private String targetBean;//当前客户端请求的Servlet名字  
  21.     private Servlet proxy;//代理Servlet  
  22.       
  23.     @Override  
  24.     public void init() throws ServletException {  
  25.         super.init();  
  26.         WebApplicationContext wac =   
  27.             WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); //初始化Spring容器  
  28.         this.targetBean = getServletName();  
  29.         this.proxy = (Servlet) wac.getBean(targetBean);//调用ServletBean  
  30.         proxy.init(getServletConfig());//调用初始化方法将ServletConfig传给Bean  
  31.     }  
  32.   
  33.     @Override  
  34.     public void service(ServletRequest arg0, ServletResponse arg1)  
  35.             throws ServletException, IOException {  
  36.         proxy.service(arg0, arg1);//在service方法中调用bean的service方法,servlet会根据客户的请求去调用相应的请求方法(Get/Post)  
  37.     }  
  38. }  

第二步:在web.xml中注册Servlet

注意这里的Servlet是有规范的一定要遵循

 1、<servlet-name>的名字必须是Spring容器中ServletBean的id

 2、<servlet-class>必须是上面写的代理类的全路径com.xwl.estore.servlet.ServletToBeanProxy

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <!-- Spring -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>  
  12.             /WEB-INF/classes/applicationContext.xml  
  13.         </param-value>  
  14.     </context-param>  
  15.     <listener>  
  16.         <listener-class>  
  17.             org.springframework.web.context.ContextLoaderListener  
  18.         </listener-class>  
  19.     </listener>  
  20.       
  21.     <!-- Servlet -->  
  22.   <servlet>  
  23.     <servlet-name>helloServlet</servlet-name>  
  24.     <servlet-class>com.xwl.estore.servlet.ServletToBeanProxy</servlet-class>  
  25.   </servlet>  
  26.   
  27.   <servlet-mapping>  
  28.     <servlet-name>helloServlet</servlet-name>  
  29.     <url-pattern>/HelloServlet</url-pattern>  
  30.   </servlet-mapping>  
  31.       
  32.    <welcome-file-list>  
  33.      <welcome-file>index.jsp</welcome-file>  
  34.    </welcome-file-list>  
  35. </web-app>  

第三步:编写ServletBean(这也是一个Servlet要继承HttpServlet)

[html]  view plain copy print ?
  1. package com.xwl.estore.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.annotation.Resource;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.context.annotation.Scope;  
  13. import org.springframework.stereotype.Controller;  
  14.   
  15. import com.xwl.estore.service.UserService;  
  16.   
  17. @SuppressWarnings("serial")  
  18. @Controller  
  19. @Scope("prototype")  
  20. public class HelloServlet extends HttpServlet {  
  21.   
  22.     private UserService userService;  
  23.       
  24.     @Resource  
  25.     public void setUserService(UserService userService) {  
  26.         this.userService = userService;  
  27.     }  
  28.   
  29.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  30.             throws ServletException, IOException {  
  31.         System.out.println("Get");  
  32.         PrintWriter out = response.getWriter();  
  33.         out.println(userService.sayHello("Hello,Spring.Servlet"));    
  34.     }  
  35.   
  36.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  37.             throws ServletException, IOException {  
  38.         System.out.println("Post");  
  39.         PrintWriter out = response.getWriter();  
  40.         out.println(userService.sayHello("Hello,Spring.Servlet"));        
  41.     }  
  42.   
  43. }  

第四步:编写Spring配置文件(applicationContext.xml)

[html]  view plain copy print ?
  1. <span style="font-size:16px;">这里我用的是annotation的方式</span>  
[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?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" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.     http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.     http://www.springframework.org/schema/context   
  10.     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.     http://www.springframework.org/schema/tx   
  12.     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  13.     http://www.springframework.org/schema/aop   
  14.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd     
  15.     ">  
  16.    <context:annotation-config></context:annotation-config>  
  17.    <context:component-scan base-package="com.xwl.estore"></context:component-scan>  
  18.   
  19. </beans></span>  
第五步:进行测试

为了测试我编写了一个Service类(一个接口和一个实现)

下面是Service接口

[html]  view plain copy print ?
  1. package com.xwl.estore.service;  
  2.   
  3. public interface UserService {  
  4.   
  5.     public String sayHello(String hello);  
  6. }  
下面是Service接口的实现

[html]  view plain copy print ?
  1. package com.xwl.estore.service.impl;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. import com.xwl.estore.service.UserService;  
  6.   
  7. @Service  
  8. public class UserServiceImpl implements UserService {  
  9.   
  10.     public String sayHello(String hello) {  
  11.         return hello;  
  12.     }  
  13. }  
在浏览器中输入Url  http://localhost:8080/EStore/HelloServlet  进行访问结果如下:

Hello,Spring.Servlet


说明:Spring整合了Servlet之后我们就可以整合Hibernate,JDBC,和事务管理等,这里只是搭建搭建一个HelloWorld。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值