小编典典

结合使用AJAX和Servlet

jsp

我刚开始使用AJAX。我不确定很多事情,尽管我在网上找到了一些教程并开始工作。对于粘贴大量代码,我深表歉意,但是对于我哪里出错了,我并没有太多想法。谢谢您的帮助!:-)

这是我的JSP(仅javascript部分)和servlet代码:(之后是异常,后跟等效的是我将用于普通Java应用程序的Java代码):

JSP:

//--Function to get the xmlhttp object
function getHttpObject(){
    var xmlhttp = null;
    if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
    else if (window.ActiveXObject){xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    else {alert("Your browser does not support XMLHTTP!");}
    return xmlhttp;
}

function populateReply(str){
    xmlhttp = getHttpObject();
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4){
            ajxfrm.chatresponse.value=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","NewServlet?chatinput="+str,true);
    xmlhttp.send(null);
}

Servlet:

public class NewServlet extends HttpServlet implements Servlet {

NetworkAimlFacade aiml = null;
int status = 0;
String botName;
String param[];
GraphBuilder builder;


public NewServlet() throws Exception{
    super();

    aiml = new NetworkAimlFacade(param);
    builder = aiml.getNetworkGraphBuilder();
    builder.addDirectoryUnlessAlreadyAdded
    (
        "C:\\Program Files\\RebeccaAIML\\aiml\\annotated_alice"
    );
    builder.createGraph();
    botName = builder.getBotPredicate("name");

}

private String handleRequest(String param){
    return null;
}

public void destroy()
{
    try{
    aiml.destroy();
    }catch(Exception e){
        e.printStackTrace();
    }
}

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

    res.setContentType("text/xml");
    res.setHeader("Cache-Control", "no-store, no-cache");
    String input = "";
    if(req.getParameter("chatinput") != null) 
        input = req.getParameter("chatinput");
    try{
        String response = builder.getResponse(input);
        if(response!=null)    res.getWriter().write(response);
            else res.getWriter().write("No");
    }catch(Exception e){
        e.printStackTrace();
    }

}

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

}}

这是我得到的错误,而不是从servlet获得响应:

Apache Tomcat / 7.0.11-错误报告

HTTP状态500-

类型 例外报告

信息

描述 服务器遇到内部错误(),导致服务器无法满足此请求。

例外

    javax.servlet.ServletException:实例化Servlet类NewServlet时出错
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)  
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)   
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)   
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394) 
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)  
    org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)    
    org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)    
    java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:886) 
    java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:908) 
    java.lang.Thread.run(Thread.java:619)

根本原因

java.lang.NullPointerException Ice.PropertiesI。<init>(PropertiesI.java:250)
    Ice.Util.createProperties(Util.java:29)
    Ice.Util.initialize(Util.java:70)
    Ice.Util.initialize(Util.java:49)
    Ice.Util.initialize(Util.java:56)
    rebecca.NetworkGraphBuilderAIML。<init>(未知来源)    
    rebecca.NetworkAimlFacade。<init>(未知来源)  
    NewServlet。<init>(NewServlet.java:27)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    java.lang.Class.newInstance0(Class.java:355)
    java.lang.Class.newInstance(Class.java:308)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)
    org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)
    java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)

note注意 在Apache Tomcat / 7.0.11日志中可以找到根本原因的完整堆栈跟踪。

Apache Tomcat / 7.0.11

这是我在普通Java应用程序中使用的等效Java代码,它工作正常!

  public class Console {

  public static void main(String args[]) {
  NetworkAimlFacade aiml = null;
  int status = 0;
  try {
    aiml = new NetworkAimlFacade(args);
    GraphBuilder builder = 
        aiml.getNetworkGraphBuilder();
    builder.addDirectoryUnlessAlreadyAdded
    (
      "C:\\Program Files\\RebeccaAIML\\aiml\\annotated_alice"
    );
    builder.createGraph();
    String botName = 
        builder.getBotPredicate("name");
    String initialResponse = 
        builder.getResponse("connect");
    System.out.println(botName + " says: " + 
        initialResponse);
    while(true) {
      System.out.print("You say> ");
      BufferedReader br = 
        new BufferedReader(new 
            InputStreamReader(System.in));
      String input = br.readLine();
      if(input.equals("/exit")) {
        break;
      } else {

String response = 
            builder.getResponse(input);
        System.out.println("=====================");
        //Print out what Rebecca says.
        System.out.println(botName + " says: " + 
            response);
        }
    }
    aiml.destroy();
    } catch(NetworkException e) {
      e.printStackTrace();
      status = 1;
    } catch (Exception e) {
      e.printStackTrace();
      status = 1;
    }
    System.exit(status);
  }
}

阅读 589

收藏
2020-06-10

共1个答案

小编典典

以下是您的例外情况的相关摘录:

javax.servlet.ServletException: Error instantiating servlet class NewServlet
  ...
java.lang.NullPointerException 
  Ice.PropertiesI.<init>(PropertiesI.java:250)
  Ice.Util.createProperties(Util.java:29)
  Ice.Util.initialize(Util.java:70)
  Ice.Util.initialize(Util.java:49)
  Ice.Util.initialize(Util.java:56)
  rebecca.NetworkGraphBuilderAIML.<init>(Unknown Source)
  rebecca.NetworkAimlFacade.<init>(Unknown Source)
  NewServlet.<init>(NewServlet.java:27)
  ...

换句话说,当容器试图做

Servlet serlvet = new NewServlet();

它失败了,因为PropertiesI在第250行的类的构造函数中,某些对象期望某个对象不存在,null并尝试直接访问它,而无需事先检查是否null存在。PropertiesI在第250行检查类的源代码,并进行相应的修复。

这些都与ajax或servlet 无关 。了解如何解释异常和堆栈跟踪。

2020-06-10