我試圖創建一個使用調用Java方法
嵌入式碼頭到主機的服務器, 並用Java腳本顯示主網頁一個簡單的HTML代碼,重量輕的web界面,因爲網頁不是靜態的,這取決於我需要調用java代碼的條件。樣本HTML代碼如下:
<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>
我不知道,如果這個問題在前面已經公佈,但我想知道有沒有傳遞到用戶定義的Java代碼中的變量,並獲得返回值和顯示的方法他們在網絡界面?
我讀了一點點我沒有使用任何外部工具,使用eclipse開發,使用applet不是一個選項。我希望網頁界面儘可能輕便。
編輯2:
我已經更新了下面給出的建議的HTML文件,但是這似乎並不爲我工作。我懷疑這是因爲我寫的處理方式,日誌信息是:
2012-05-28 16:02:53.753:DBUG:oejs.AsyncHttpConnection:async request (null null)@16471729 [email protected]
2012-05-28 16:02:53.754:DBUG:oejs.Server:REQUEST/on org.eclipse.jett[email protected][email protected]: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 [email protected]
爲處理器編寫的代碼如下
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();
當你說它不工作,發生了什麼?另外,如果有堆棧痕跡,您是否也可以包含這些內容? – jmort253
@ jmort253 - 哎呀對不起,我忘了提到發生了什麼,localhost:8080加載文本和按鈕,當我鍵入say 1並單擊提交按鈕時,我沒有看到網頁上的任何更改,但只有日誌控制檯中的日誌消息。 – bhavs
好的。調試101。首先,確保你在你的Handler中做了些什麼來打印日誌。 'System.out.println(「Hello world !!!」);'如果你沒有設置日誌記錄,'是一個非常快速的方法。其次,檢查Firebug或Chrome調試器中的NET選項卡,並查看服務器響應,看它是否包含服務器在'response.getWriter.println()'中返回的Hello world。換句話說,您需要通過確定斷開連接的位置來縮小問題的範圍。 – jmort253