Servlet本身就是一个class,通过new一个实例,调用其中的doPost(),doGet(),destory().....方法

解决方案 »

  1.   

    有资料说:
    Servlet可被链接(chain)。一个Servlet可以调用另一个或一系列Servlet,即成为它的客户端。 但我不知道如何做
      

  2.   

    你可以在这个 SERVLET 被初始化时将其放入 ServletContext 中,然后就可以在其它地方调用了:
    voi init(ServletConfig cfg){
      context.setAttribute("serv", this);
    }Servlet s = (Servlet)application.getAttribute("serv");
    当然,可以考虑加上判断它是否为空的语句。
      

  3.   

    package com.dayang.util;import java.io.IOException;import java.net.URL;
    import java.net.URLConnection;
    import java.net.MalformedURLException;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;
    import java.util.ArrayList;/**
     * 客户端调用servlet的类,参数用对象的方式传递,servlet的返回也是java对象
     * User: zhouhb
     * Date: 2003-7-30
     * Time: 15:51:14
     * To change this template use Options | File Templates.
     */
    public class ClientRequest { private static URL servletURL; public static void setCodeBase(URL codeBase)
            throws ClientRequestException {
    try {
    servletURL = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), codeBase.getFile() + "/xmlquery");
    } catch (MalformedURLException e) {
    e.printStackTrace();
    throw new ClientRequestException(e);
    }
    } public static ArrayList sendRequst(int cmd, ArrayList params)
            throws ClientRequestException {
    ArrayList results = new ArrayList();
    try {
    URLConnection servletConnection = servletURL.openConnection();     //建立与Servlet的联接 servletConnection.setDoOutput(true);            //用于写数据
    servletConnection.setUseCaches(false);      //这次连接不用Cache ObjectOutputStream oos = new ObjectOutputStream(servletConnection.getOutputStream());
    oos.writeInt(cmd); int paramCount = params.size();
    oos.writeInt(paramCount);
    for (int i = 0; i < paramCount; i++) {
    oos.writeObject(params.get(i));
    }
    oos.flush(); ObjectInputStream ois = new ObjectInputStream(servletConnection.getInputStream());
    int resultCount = ois.readInt();
    for (int i = 0; i < resultCount; i++) {
    Object ob = ois.readObject();
    results.add(ob);
    }
    return results;
    } catch (IOException e) {
    e.printStackTrace();
    throw new ClientRequestException(e);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    throw new ClientRequestException(e);
    }
    }}