我刚刚开始使用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 - 错误报告
类型异常报告
消息
描述服务器遇到内部错误(),导致服务器无法完成此请求。
异常
javax.servlet.ServletException: Error instantiating servlet class 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>(Unknown Source) rebecca.NetworkAimlFacade.<init>(Unknown Source) NewServlet.<init>(NewServlet.java:27) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 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)
注意 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);
}
}
答案 0 :(得分:3)
以下是您例外的相关摘录:
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
或不。检查第250行的PropertiesI
类的源代码并相应地修复它。
这与ajax或servlet都有无。学习如何解释异常和堆栈跟踪。