Servlet在另一个服务器中调用类的方法

时间:2016-12-18 18:20:13

标签: java servlets

我是JAVA servlet / server编程的新手。我试图让servlet从服务器调用实例化类的getInstance()方法。我已粘贴在服务器代码下面,该代码注册客户端类并实例化它。这样运行没有错误

public class RelevanceClient {

private static RelevanceClient client;
public final static String LOGSOURCE = "RELEVANCECLIENT";

private HistogramClient histogramClient;
private UserClient userClient;
private LoadBalancer loadBalancer = null;

static {
    try {
        SystemRegistrar.register();
    }
    catch (Exception e) {
        Object args[] = {e};
        Log.logException(e);
        Log.logMessage(LOGSOURCE, Log.ERROR,
            RelevanceClientRes.get(RelevanceClientRes.FAILED_TO_REGISTER, args));
        client = null;
    }
}

public static void registerClient(RelevanceClient _client) { client = _client; }

/**
 * Returns instance of RelevanceClient
 */
public static RelevanceClient getInstance() throws RelevanceClientException {
    if (client == null)
        throw new RelevanceClientException(
            RelevanceClientRes.get(RelevanceClientRes.CLIENT_INIT_ERR));
    return client;
}

现在,我有一个servlet代码调用getInstance()来获取类。我得到一个客户端类没有注册错误,即使我在服务器中注册客户端时遇到没有错误。

public class Relevance extends HttpServlet {

private final static String P_CMD    = "cmd";

public final static String SERVLET_INFO = "eSelf relevance servlet";

ServletConfig config = null;
RelevanceClient client = null;

private Hashtable commandMap = new Hashtable();

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    this.config = config;
    try {
        client = RelevanceClient.getInstance();
    } catch(Exception rootCause) {
        client = null;
        throw new ServletException(ServletRes.get(ServletRes.RCLIENT_INIT_ERR), rootCause);
    }
    Log.logMessage(Log.INFO, ServletRes.get(ServletRes.SERVLET_INIT));
}

我在某些方面做错了什么或非法?我很感激任何线索。完整的错误消息如下所示。

javax.servlet.ServletException: Error obtaining relevance client
eself.tool.servlet.Relevance.init(Unknown Source)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

根本原因:

eself.client.relevance.RelevanceClientException: Relevance client was not registered successfully
eself.client.relevance.RelevanceClient.getInstance(Unknown Source)
eself.tool.servlet.Relevance.init(Unknown Source)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

RelevanceClient.client在代码中始终为null。因此,初始化将始终失败。