I have just begun with AJAX. I'm not sure about many things, though I picked up a few tutorials on the internet and started working. I apologize for pasting a lot of code, but I don't have much of an idea as to where am I going wrong. Thank you for your help though! :-)
Here is my JSP(only the javascript part) and servlet code :(followed by the exception and followed by equivalent the Java code that I would use for a normal Java application) :
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 {
}}
Here's the error that I get, instead of getting the response from the servlet:
Apache Tomcat/7.0.11 - Error report
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
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)
root cause
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)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.11 logs.
Here's the equivalent Java code, that I use in a normal Java application, and it works fine!
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);
}
}
Here's an extract of relevance from your exception:
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)
...
In other words, when the container tried to do
Servlet serlvet = new NewServlet();
It failed, because something in the constructor of PropertiesI
class, at line 250, expected some object to be not null
and tried to access it directly without checking beforehand if it's null
or not. Check the source code of PropertiesI
class at line 250 and fix it accordingly.
This all has nothing to do with ajax or servlets. Learn how to interpret exceptions and stacktraces.