servletlistener.java~16~

来自「一个java」· JAVA~16~ 代码 · 共 66 行

JAVA~16~
66
字号
package bookstore.servlet;

import bookstore.util.*;

import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.rmi.PortableRemoteObject;

public final class ServletListener extends HttpServlet implements ServletContextListener
{
  private ServletContext servletContext=null;
  private EJBGetterSession ejbGetterSession;
  private EJBGetterSessionHome ejbGetterSessionHome;

  public void contextInitialized(ServletContextEvent sce)
  {
    try
      {
        this.servletContext = sce.getServletContext();
        Context context = getInitialContext();
        Object ref = context.lookup("EJBGetterSession");
        //look up jndi name and cast to Home interface
        ejbGetterSessionHome = (EJBGetterSessionHome) PortableRemoteObject.narrow(
            ref, EJBGetterSessionHome.class);
        ejbGetterSession = ejbGetterSessionHome.create();
        servletContext.setAttribute("ejbGetterSession",ejbGetterSession);
      }catch(Exception ex)
      {
        System.out.println(ex.getMessage());
      }
  }
  //Notification that the servlet context is about to be shut down
  public void contextDestroyed(ServletContextEvent sce)
  {
    servletContext.removeAttribute("ejbGetterSession");
    this.servletContext=null;
  }

  private Context getInitialContext() throws Exception {
      System.out.println("TestClient getInitialContext");
      String url = "t3://samson:7001";
      String user = "weblogic";
      String password = "weblogic";
      Properties properties = null;
      try {
        properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        properties.put(Context.PROVIDER_URL, url);
        if (user != null) {
          properties.put(Context.SECURITY_PRINCIPAL, user);
          properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
        }

        System.out.println("getInitialContext out");
        return new InitialContext(properties);
      }
      catch(Exception e) {
        throw e;
      }
    }//getInitailContext


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?