Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HttpServer not dispatching (or receiving?) Ajax requests - Help needed

843790Oct 18 2007 — edited Jun 25 2008
Hello,

I have run into a strange problem. Strange because the combination of com.sun.net.httpserver.HttpServer with Internet Explorer works whereas the combination of com.sun.net.HttpServer with Firefox and Opera doesn't work!

To illustrate I have written a small Java program that implements a HttpHandler that I want to serve Ajax requests that are fired from Javascript. I use the prototype.js Javascript library to send the Ajax requests.

The webpage fires a Ajax request every second. The HttpHandler responds with a <script> block that calls the paint() function on the webpage.

This works fine on Internet Explorer 6, but Firefox and Opera don't get an answer on the Ajax requests that the Javascript sends. Even more, the request doesn't event make it to the MyHandler.handle() method. How can I test whether the request even makes it to the HttpServer object, and if it reaches the HttpServer object, then why isn't it dispatched to my HttpHandler?

I hope I made myself clear and that someone can point out to me what I am doing wrong or what the code that I wrote is lacking to make it work with Firefox and Opera.

Thanks a whole lot in advance!

Here is the code:

File TestHttpServerWithPrototypeJS.html (run it on IIS or Apache or whatever)

<html>
<head>
<title>Test HttpServer with prototype.js</title>
<script type="text/javascript" src="http://localhost/prototype.js"></script>
<script>
function uncache(url)
{
return url + '?time=' + (new Date()).getTime();
}
function initialize()
{
try
{
// Make a HTTP request every second to /myapp and update the mydiv1 element.
// What is written into the mydiv1 is a <script> section that calls the paint function.
// The paint function then updates the mydiv2 element with a counter value that
// is incremented by the server code after each http (ajax) request.
var myAjax = new Ajax.PeriodicalUpdater(
'mydiv1',
uncache('http://localhost:8000/myapp'),
{
method: 'post',
asynchronous: true,
frequency: 1,
evalScripts: true
});
}
catch(e)
{
alert('Exception: ' + e);
}
}
function paint(data)
{
$(mydiv2).innerHTML = data;
}
window.onload = initialize;
</script>
</head>
<body>
<div id='mydiv1'></div>
<div id='mydiv2'></div>
</body>
</html>




File TestHttpServer.java:

import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;

public class TestHttpServer
{
public static void main(String[] args)
{
HttpServer server = null;
try
{
server = HttpServer.create(new InetSocketAddress(8000), 100);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
server.createContext("/myapp", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
}


File MyHandler.java:

import java.io.IOException;
import java.io.OutputStream;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

class MyHandler implements HttpHandler
{
int counter = 0;
public void handle(HttpExchange t) throws IOException
{
try
{
String response = "<script type=\"text/javascript\">paint('server side counter = "+ counter + "')</script>";
counter++;

t.sendResponseHeaders(200, response.length());

OutputStream os = t.getResponseBody();

os.write(response.getBytes());
os.close();
}
catch (Exception e)
{
int a_variable_on_which_a_breakpoint_can_be_set = 0;
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 23 2008
Added on Oct 18 2007
3 comments
374 views