I am trying to create a light weight web interface using
embedded jetty to host the server,
and a simple html code with a java script to display the main page, since the pages are not static depending on a condition I would need to make a call to a java code. The sample html code is as follows :
<body>
<script type="text/javascript">
function myfunction(frm)
{
var opt=frm.option.value;
alert("option is"+frm.option.value);
// call a java method depending on the value of opt
frm.option.value="";
}
</script>
<h1 style="text-align: center;">Agent Management Interface</h1>
<ol>
</ol>
<form name="management_form">
Enter Option: <input type="text" id="optiontb" name="option">
<input type="button" onclick="myfunction(this.form)" value="submit">
</form>
</body>
</html>
I am not sure if this question has been posted earlier but I am wondering is there a method to pass a variable to a user defined java code and obtain return values and display them on the web interface?
I read around a little bit I am not using any external tool, developing using eclipse, using applets is not an option. I would like to the web interface as light weight as possible.
Edit 2:
I have updated the html file with the suggestions as given below, but this does not seem to be working for me. I suspect it is because of the way I have written the handlers, the log messages are :
2012-05-28 16:02:53.753:DBUG:oejs.AsyncHttpConnection:async request (null null)@16471729 org.eclipse.jetty.server.Request@fb56b1
2012-05-28 16:02:53.754:DBUG:oejs.Server:REQUEST / on org.eclipse.jetty.server.nio.SelectChannelConnector$SelectChannelHttpConnection@bc8e1e@127.0.0.1:8080<->127.0.0.1:47830
2012-05-28 16:02:53.756:DBUG:oejs.Server:RESPONSE / 304
2012-05-28 16:02:53.757:DBUG:oejs.AsyncHttpConnection:async request (null null)@16471729 org.eclipse.jetty.server.Request@fb56b1
The code written for the handlers is as follows
System.setProperty("org.eclipse.jetty.util.log.DEBUG","true");
Server server = new Server(8080);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setResourceBase(args.length == 2?args[1]:".");
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
System.out.println("serving " + resource_handler.getBaseResource());
ContextHandler context0 = new ContextHandler();
context0.setContextPath("/senddata");
Handler handler0=new HelloHandler();
context0.setHandler(handler0);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[]{context0});
HandlerCollection handlersc = new HandlerCollection();
handlersc.setHandlers(new Handler[]{resource_handler,new DefaultHandler(), contexts});
server.setHandler(handlersc);
server.start();
server.join();
The technique that you’re looking for is AJAX. Since JavaScript is client side code and Java is code that runs on the server, the only way to get data to or from the server is to make an HTTP request to the server to request the data.
Below is an example from the Mozilla Developer Center page on Getting Started with AJAX:
While the above code will allow you to make the HTTP request from the browser, you’ll need a servlet in your Java application in order to receive the request, process the request, and return the response back to the browser.
The Embedded Jetty site has an example of how to create a Handler, which you can use to examine an HTTP request, process it, and return a response. I modified the example slightly to extract a query parameter that you would pass in through the AJAX request: