📄 servletlistener.java~16~
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -