
package mypackage1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;

public class MyServlet1 extends HttpServlet 
{
  private static final String CONTENT_TYPE = &quot;text/html; charset=windows-1252&quot;;

  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    doPost(request,response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println(&quot;&lt;html&gt;&quot;);
    out.println(&quot;&lt;head&gt;&lt;title&gt;MyServlet1&lt;/title&gt;&lt;/head&gt;&quot;);
    out.println(&quot;&lt;body&gt;&quot;);
    out.println(&quot;&lt;p&gt;The servlet has received a request. This is the reply.&lt;/p&gt;&quot;);
      try { 

        Hashtable env = new Hashtable();
        // Connection to J2EE EJB Server 
        // Click here for BEA, IBM, IONA, Oracle and others
        String para=&quot;&lt;P&gt;&quot;;
        out.println(para+&quot;Start: Preparing environment to connect to J2EE EJB AppServer&quot;);
        env.put(Context.INITIAL_CONTEXT_FACTORY,
        &quot;weblogic.jndi.WLInitialContextFactory&quot;);
        env.put(Context.PROVIDER_URL, &quot;t3://localhost:7001&quot;);        
        out.println(para+&quot;Creating JNDI Context&quot;);

        Context jndiContext = new InitialContext(env);

        out.println(para+&quot;Doing a JNDI Lookup on Home Interface&quot;);

        // The name you put for jndiContext.lookup() must match the &lt;jndi-name&gt;
        // in ejb-jar.xml in the deployment jar.       
        MySessionEJBHome sessionHome=(MySessionEJBHome) jndiContext.lookup(&quot;MySessionEJB&quot;);
        
        out.println(para+&quot;Create Remote Reference&quot;);        
        Object ref = sessionHome.create();

        out.println(para+&quot;Now, narrow to a session bean.&quot;);        
        MySessionEJB sessionEJB 
          = (MySessionEJB) PortableRemoteObject.narrow(ref, MySessionEJB.class);

        String info = sessionEJB.getInfo();

        out.println(para+&quot;We have an data from the session bean: &quot;+info); 
  } catch (Exception e) {
        out.println(&quot;Encountered error &quot;+e); 
        e.printStackTrace(System.err);

  }    out.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
    out.close();

} // End doPost
} 