• j2ee 获取上下文环境


    j2ee 获取上下文环境

    问题:1.不访问服务,如何获取上下文环境?

    解答:1.通过在web里面配置servlet来解决。

    1.1 web.xml

    1     <servlet>
    2         <servlet-name>GetContext</servlet-name>
    3         <servlet-class>com.ge.context.GetContext</servlet-class>
    4         <load-on-startup>1</load-on-startup>
    5     </servlet>

    1.2GetContext.java

     1 package com.ge.context;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 import org.springframework.web.context.WebApplicationContext;
    11 import org.springframework.web.context.support.WebApplicationContextUtils;
    12 
    13 public class GetContext extends HttpServlet {
    14     private static final long serialVersionUID = 1L;
    15 
    16     public GetContext() {
    17         super();
    18     }
    19 
    20     public void init() {
    21         WebApplicationContext context = WebApplicationContextUtils
    22                 .getWebApplicationContext(this.getServletContext());
    23         ContextHolder.getInstance().setApplicationContext(context);
    24         ContextHolder.getInstance().setServletContext(this.getServletContext());
    25     };
    26 
    27     protected void doGet(HttpServletRequest request,
    28             HttpServletResponse response) throws ServletException, IOException {
    29         init();
    30     }
    31 
    32     protected void doPost(HttpServletRequest request,
    33             HttpServletResponse response) throws ServletException, IOException {
    34         init();
    35     }
    36 }

    1.3ContextHolder.java

     1 package com.ge.context;
     2 
     3 import javax.servlet.ServletContext;
     4 
     5 import org.springframework.context.ApplicationContext;
     6 
     7 public class ContextHolder {
     8     private final static ContextHolder instance = new ContextHolder();
     9     private ApplicationContext ac;
    10     // added by cobble.ge 20130415
    11     private ServletContext servletContext;
    12 
    13     private ContextHolder() {
    14     }
    15 
    16     public static ContextHolder getInstance() {
    17         return instance;
    18     }
    19 
    20     public synchronized void setApplicationContext(ApplicationContext ac) {
    21         this.ac = ac;
    22     }
    23 
    24     public ApplicationContext getApplicationContext() {
    25         return ac;
    26     }
    27 
    28     public ServletContext getServletContext() {
    29         return servletContext;
    30     }
    31 
    32     public synchronized void setServletContext(ServletContext servletContext) {
    33         this.servletContext = servletContext;
    34     }
    35 }

    ------------

    @cobble HF.AH.CHN 2013-04-18

    资料1:互联网,忘了从哪位仁兄的文章看的了。

  • 相关阅读:
    SQL游标操作每隔5分钟时间段数据统计信息
    win64位操作系统下安装pl/sql developer 并登录连接到oracle12c
    分科目统计每科前三名的学生
    merge源表数据移植到目标表新表数据中
    sqlserver表分区
    用SqlBulkCopy批量插入数据到SqlServer数据库表中
    SQL server插入数据后,如何获取自增长字段的值?
    Java创建线程的三种方式
    Java用户线程和守护线程
    Java虚拟机详解
  • 原文地址:https://www.cnblogs.com/cobble19/p/3029288.html
Copyright © 2020-2023  润新知