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>