/* $Id: CookieExample.java,v 1.2 2001/06/11 22:49:11 craigmcc Exp $
 *
 */
package examples.servlets;
import java.sql.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import commonServlet.CmRequestPatten;
import sample.*;/**
 * Example servlet showing request headers
 *
 * @author James Duncan Davidson <[email protected]>
 */public class CommonServlet extends HttpServlet {  protected ServletConfig   servletConfig;
  protected ServletContext  servletContext;  public void init(ServletConfig argServletConfig)throws ServletException {
    super.init(argServletConfig);
    servletConfig = argServletConfig;
    servletContext = argServletConfig.getServletContext();
  }    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
      String applicationClass = (String)request.getParameter("applicationClass");
      String worningPage = (String)request.getParameter("worningPage");
      String nextPage = (String)request.getParameter("nextPage");
      String excuteType = (String)request.getParameter("excuteType");
      HttpSession session = request.getSession(false);
      try {
        if (applicationClass != null && applicationClass.trim().length() != 0) {
          CmRequestPatten application = (CmRequestPatten)Class.forName(applicationClass).newInstance();
          application.setHttpRequest(request);
          application.setHttpResponse(response);
          application.setHttpSession(session);
          application.setServletConfig(servletConfig);
          application.setServletContext(servletContext);
          if (excuteType.equals("select")) {
            application.selectData();
          } else if (excuteType.equals("insert")) {
            application.insertData();
          } else if (excuteType.equals("updata")) {
            application.updata();
          } else if (excuteType.equals("delete")) {
            application.deleteData();
          } else {
            getServletContext().getRequestDispatcher(worningPage).forward(request,response);
          }
        } else {
          getServletContext().getRequestDispatcher(worningPage).forward(request,response);
        }      } catch (Throwable ex) {
        System.out.println(ex);
        getServletContext().getRequestDispatcher(worningPage).forward(request,response);
      }
      getServletContext().getRequestDispatcher(nextPage).forward(request,response);
    }    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }}

解决方案 »

  1.   

    /* $Id: CookieExample.java,v 1.2 2001/06/11 22:49:11 craigmcc Exp $
     *
     */
    package sample;
    import ejb.*;
    import commonServlet.CmRequestPatten;
    import java.sql.*;
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import datasource.sampleDataSource;import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import java.lang.reflect.*;
    import java.text.*;
    import java.util.*;
    import javax.ejb.*;
    import javax.naming.*;
    import javax.rmi.*;
    import javax.xml.parsers.ParserConfigurationException;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    /**
     * Example servlet showing request headers
     *
     * @author James Duncan Davidson <[email protected]>
     */public class samplesource extends samplesource0 {  protected static final String ENV_ENTRY_ENVIRONMENT_FILE = "ome.Properties";
      protected String rootUri;
      protected Vector  environmentList = new Vector();
      protected HashMap includeFileMap = new HashMap();  public samplesource() {   
         }  public void selectData() throws Throwable {    String homeName = (String)httpServletRequest.getParameter("jNDIHomeName");
        String homeInterfaceName = (String)httpServletRequest.getParameter("ejbHome");
        Class homeInterface = null;
        if (homeInterfaceName != null) {
          try {
            homeInterface = Class.forName(homeInterfaceName);
          } catch (ClassNotFoundException ex) {
          }
        }   
        EJBHome home = null;
        try {
          Object homeObject = getInitialContext().lookup(homeName);
          if (homeInterface == null) {
            homeInterface = homeObject.getClass();
          }
          home = (EJBHome)PortableRemoteObject.narrow(homeObject, homeInterface);
        } catch (NamingException ex) {
          //名前が適切でない場合    }    Method homeMethod = home.getClass().getMethod("create", new Class[]{});    EJBObject remote =  (EJBObject)homeMethod.invoke(home, new Object[]{});
        List list = null;
        if (remote instanceof ejbsample) {
          list = ((ejbsample)remote).selectData(httpServletRequest);
        }
        httpServletRequest.setAttribute("result" ,list);
      }  private Context getInitialContext() throws NamingException {
        try {
          // Get an InitialContext
          String url = servletConfig.getInitParameter("EJBProviderURL");
          String EJBInitialContextFactory = servletConfig.getInitParameter("EJBInitialContextFactory");
          Properties h = new Properties();
          h.put(Context.INITIAL_CONTEXT_FACTORY, EJBInitialContextFactory);
          h.put(Context.PROVIDER_URL, url);
          return new InitialContext(h);
        } catch (NamingException ne) {
          throw ne;
        }
      }}
      

  2.   

    1 如何在servlet中调用不同的html
      例如:在一个用户验证的servlet中,如果用户验证成功,则打开success.html,否则打开error.html
    if ok response.sendRedirect("ok.html") else response.sendRedirect("error.html");
    2 在一个ejb中如何调用另一个ejbHashtable ht = new Hashtable();
    ht.put(ctx.PROVIDER_URL, "t3://ip:port");
        ht.put(ctx.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        ctx = new InitialContext(ht);
    anEJBHome = (EJBHome) javax.rmi.PortableRemoteObject.narrow (ctx.lookup (homeClass.getName()), homeClass);
         
    3 在一个servlet中如何调用另一个servlet
      没这么调的.没试过
    4 在一个jsp中如何调用另一个jsp
      <%@ include file="xxx.jsp" %>
     还有一种,没有用过,懒得找了 :)