I do have a communication problem here between my Java servlet and an Ajax request. more about it:
Absolute path to the index.html (including JavaScript/Ajax request):
http://localhost:9080/chat/index.html
In the same folder the servlet:
MyChat.class
And the Request is working like this:
var url = "http://localhost:9080/chat";
var name = document.getElementById("username").getAttribute("value");
var message = document.getElementById("message").getAttribute("value");
var tosend = name+","+message;
request.open('GET', url, true);
request.send(tosend);
request.onreadystatechange = interpretRequest;
I’m having a formula where a user just types in the name and the message and username and message are <input> tags in my HTML file. The Ajax request works, that’s sure, but it doesn’t communicate with the servlet. I also don’t have an idea where the output from System.out.println() goes. No log file is filled… And the servlet looks like this:
public class MyChat extends HttpServlet
{
private static final long serialVersionUID = 1L;
private ArrayList<String> myMessages = new ArrayList<String>();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
BufferedReader r = request.getReader();
while(r.readLine() != null)
{
// split the words at the ','
String[] tmp = r.readLine().split(".\\s");
myMessages.add(tmp[0]+" "+tmp[1]);
}
//response.setContentType("text/html");
PrintWriter out = response.getWriter();
Iterator<String> it = myMessages.iterator();
while(it.hasNext())
{
out.println(it.next());
System.out.println(it.next());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
The URL you’ve specified isn’t to your servlet.
Just like the URL to the index page is
http://<server>:<port>/<webapp name>/<resource>the URL to your servlet needs a mapping in theweb.xmlfile that corresponds to the<resource>part of the url.For example, if you had a controller servlet you’d expect something like the following in your web.xml:
The URLs that would invoke the ‘controller’ servlet would then follow the form
http://<server>:<port>/<webapp name>/<anything>.do.