jQueryを使おう!あなたの仕事が楽になります!これらのタブでクリック ハンドラーを登録し、パラメーターをタブ ID としてサーブレットに ajax リクエストを送信するだけです。とても簡単です。以下はコードです:
仮定: Tab1: ID='tab1', CLASS='tab' Tab2: ID='tab2', CLASS='tab'
// click handler
$('.tab').click(function(){
// get tab id
var id = $(this).attr('id');
// now send an ajax request to servlet with parameter which stores this id value
$.ajax({
type: 'GET', // request type
data: {param: id},
url: '/servleturl',
dataType: 'text', // or json or any other which your servlet will return
// this will be executed when request is successful and a correct response is recieved
success: function(response){
alert(response); // or do whatever you like to do with response
},
// this function will be executed when there is any error
error: function(){
},
// this will be always executed, like a finally block in Java
complete: function(){
},
});
});
エラーと完全な関数を省略できます。必要なことはすべてお伝えしました。とてもシンプルです。
使用するサーブレットで ID を受け取るには:
String tabId = request.getParameter('param');
「param」、指定したため:
data: {param: id}
「param」以外の名前を使用する場合は、代わりにそれを使用してください。
サーブレットから応答を送信するには、次を使用します。
PrintWriter out = response.getWriter();
out.write("ABCD or whatever you want");
out.close(); // DON'T forget this!