views:

667

answers:

1

hi there,

i do have a communication probleme 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 formular where a user just types in the name and the message and "username" and "message" are 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);
}

}

+2  A: 

The URL you've specified isn't to your servlet.

Just like the URL to the index page is http://&lt;server&gt;:&lt;port&gt;/&lt;webapp name>/<resource> the URL to your servlet needs a mapping in the web.xml file 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:

<servlet>
  <servlet-name>controller</servlet-name>
  <servlet-class>ControllerServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>controller</servlet-name>
  <url-pattern>/*.do</url-pattern>
</servlet-mapping>

The URLs that would invoke the 'controller' servlet would then follow the form http://&lt;server&gt;:&lt;port&gt;/&lt;webapp name>/<anything>.do.

Nick Holt
just added the web.xml. it's within the folder where the html file is and my class file. as i start the request i'm getting a request status that is not 200 so ... request fails
vlad
The web.xml file need to be in a WEB-INF directory in the root of the web app. Take a look at this tutorial for the details on packaging a web app - http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents.html
Nick Holt
and where do i have to place the class file then? :-(
vlad
The classes go in WEB-INF/classes. If you use ANT the WAR task will do it all for you.
Nick Holt