0
// 的index.jsp在JSP/AJAX
<html>
<head>
<title>JSP page</title>
<script="text/javascript">
$(function() {
// subscribe to the keydown event
$('#text1').keydown(function(e) {
// when a key is pressed check if its code was 9 (TAB)
if (e.which == 9) {
// if TAB was pressed send an AJAX request
// to the server passing it the currently
// entered value in the first textbox
$.ajax({
url: '/someservlet/',// i have created a servlet named as someservlet
type: 'POST',
data: { value: $(this).val() },
success: function(result) {
// when the AJAX succeeds update the value of the
// second textbox using the result returned by the server
// In this example we suppose that the servlet returns
// the following JSON: {"foo":"bar"}
$('#text2').val(result.foo);
}
});
}
});
});
</script>
</head>
<input type="text" id="text1" name="firsttextbox"/>
<input type="text" id="text2" name="secondtextbox"/>
<body>
// 做後到來()自動完成錯誤someservlet
String dd = request.getParameter("firsttextbox");
String json = new Gson().toJson(options);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
我是否需要導入內部頭上的任何js文件塊?誰能告訴我我錯在哪裏?編譯時,我得到了jsper ...錯誤。相同的代碼工作在小提琴:
雖然在NetBeans編譯,我收到錯誤。
任何幫助表示讚賞。
感謝您的答案很多運行...我會盡快回復 – Tom 2012-02-06 13:24:07
你能告訴我應該在servlet中做什麼修改嗎?只需在第一個文本框中輸入一些數據,我想自動填充第二個文本框,其中數據來自servlet。 – Tom 2012-02-06 13:36:36
http://stackoverflow.com/questions/5923638/how-to-return-multiple-json-objects-from-java-servlet-using-one-ajax-request希望這會幫助你 – run 2012-02-07 04:56:44