Spring Web开发



1,jar包
 导入Spring开发基本jar包
 spring-beans-3.2.0.RELEASE.jar
 spring-context-3.2.0.RELEASE.jar
 spring-core-3.2.0.RELEASE.jar
 spring-expression-3.2.0.RELEASE.jar
 导入commons-logging的jar包
 commons-logging-1.1.1.jar
 
 导入Spring web开发jar包
 spring-web-3.2.0.RELEASE.jar
 
2,applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="departmentServlet" class="cn.itcast.spring.bean.DepartmentServlet"></bean>
 </beans>


3,bean.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="applicationContext.xml"/><!--嵌入xml文件,可以合并-->
 </beans>

4,DepartmentServlet.java

 public class DepartmentServlet extends HttpServlet { 
  public void getDeartpent(){
   System.out.println("正在获取depratment");
  } 
 }
5,UserServlet.xml

 public class UserServlet extends HttpServlet { 
  private ApplicationContext applicationContext;  
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   doPost(request, response);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   DepartmentServlet httpServlet=(DepartmentServlet) applicationContext.getBean("departmentServlet");   
   httpServlet.getDeartpent();   
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
   out.println("<HTML>");
   out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
   out.println("  <BODY>");
   out.print("    spring 的页面.. ");
   out.print(this.getClass());
   out.println(", using the POST method");
   out.println("  </BODY>");
   out.println("</HTML>");
   out.flush();
   out.close();
  }  
  public void init(ServletConfig config) throws ServletException {//ServletContext
   applicationContext=new ClassPathXmlApplicationContext("bean.xml");
  }
 }

6,web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="2.4" 
  xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
   
  <!-- 在tomcat 启动的时候解析spring 的配置文件.... -->  
  <!-- spring 启动类.... -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:bean.xml</param-value>
  </context-param>
   -->
  
  <!-- listener,servlet,filter -->
    <servlet>
      <servlet-name>UserServlet</servlet-name>
      <servlet-class>cn.itcast.spring.user.UserServlet</servlet-class>
    </servlet>  
    <servlet-mapping>
      <servlet-name>UserServlet</servlet-name>
      <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 </web-app>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值