Spring在Web项目中的应用-添加ServletContext监听器
来源:互联网 发布:域名检测接口 编辑:程序博客网 时间:2024/06/01 08:23
添加ServletContext监听器
ServletContext初始化时创建Spring容器
获取容器
package com.hk.ssh.servlets;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.web.context.WebApplicationContext;import com.hk.ssh.beans.Student;import com.hk.ssh.service.IStudentService;public class RegisterServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String ageStr = request.getParameter("age"); Integer age = Integer.valueOf(ageStr); Student student = new Student(name,age); //1、创建Spring 容器(这种创建方式每来一次请求就会创建一个ApplicationContext) //ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); /** * 采用如下方式创建Spring容器,在ServletContext初始化是创建容器 * ServletContext只初始化一次,所以整个web运用都只会有一个Spring容器 */ ServletContext application = request.getSession().getServletContext(); WebApplicationContext ac = (WebApplicationContext) application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); //2、从容器中获取Service对象 IStudentService service = (IStudentService) ac.getBean("studentService"); //3、调用Service的addStudent() service.addStudent(student); request.getRequestDispatcher("/welcome.jsp").forward(request, response); }}
获取Spring容器还有一个方法
WebApplicationContext ac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 加载Spring配置文件 文件的位置和名称是可变的,且配置文件可以有多个 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 注册ServletContext监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>Register-Servlet</servlet-name> <servlet-class>com.hk.ssh.servlets.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Register-Servlet</servlet-name> <url-pattern>/registerServlet</url-pattern> </servlet-mapping></web-app>
完整项目源码:http://pan.baidu.com/s/1slEY7JR
0 0
- Spring在Web项目中的应用-添加ServletContext监听器
- quartz在web项目中的应用(不使用spring)
- spring项目中,web.xml中的 ContextLoaderListener监听器的原理
- Spring在Web中的应用
- 监听器过滤器拦截器在web项目中的作用位置
- ServletContext读取web应用中的资源文件
- ServletContext 读取Web应用中的资源文件
- 给web项目添加自定义监听器
- Spring在Web应用中的初始化
- spring在web应用中的配置
- Spring在Web应用中的配置
- Spring 在web应用中的定时器
- Spring在web开发中的应用
- Spring在web开发中的应用
- 浅谈Fit在Web项目中的应用
- 浅谈Fitnesse在Web项目中的应用
- Spring在项目中的应用(一)
- spring quartz在项目中的应用实例
- 十、今年暑假不AC
- 功能点五种方法比较
- 通过 SQL Anywhere Profiler 来对应用程序进行故障排除及调优
- 计算机视觉领域的教授、专家及研究所官方主页
- Linux中对MySQL优化
- Spring在Web项目中的应用-添加ServletContext监听器
- meta name="viewport" content="width=device-width,initial-scale=1.0" 解释
- HashMap的工作原理
- java实验3.(2)包的访问权限
- Qt自定义的无边框Dialog 在点击其他窗口时处理闪烁效果
- CentOS的ssh sftp配置及权限设置
- 线程同步理解
- Android 调用系统相机拍照并且显示在相册中,以及中间可能会遇到的一些问题的解决
- LESS