2014-11-08 213 views
0

我想從相同的JSP導航到多個Servlet。如何從同一個JSP文件向多個Servlet發送請求?

例如,

<form name="sample" action = "actionFirst" method="get"> 

點擊按鈕後(假設按鈕名稱是'Register'),請求轉到'actionFirst'Servlet。

有一個anather Servlet'actionSecond',現在我想從Javascript函數中點擊按鈕按鈕(假設按鈕名稱是'Edit')後,從同一個JSP文件傳送請求到'actionSecond' 。我怎麼能實現這個目標?

+0

可能你需要第二個按鈕或第二個表單或另一個servlet。一般不清楚你在問什麼。 – 2014-11-08 12:33:35

+0

我只想知道如何從同一個JSP文件中調用多個servlet? – 2014-11-08 12:57:59

回答

1

你可以試試這個。

<!-- create the form --> 
<form name="Form1" method="post"> 

<!-- Add the data entry bits --> 
Your Name <input type="text" name="text1" size="10" /><br /> 

<!-- Add some buttons --> 
<INPUT type="button" value="Button1" name=button1 onclick="return OnButton1();"> 
<INPUT type="button" value="Button2" name=button2 onclick="return OnButton2();"> 

<!-- close the form --> 
</form> 

腳本會是什麼樣子

<script language="Javascript"> 
<!-- 
function OnButton1() 
{ 
    document.Form1.action = "Page1.java" 
    document.Form1.target = "_blank"; // Open in a new window 
    document.Form1.submit();    // Submit the page 
    return true; 
} 

function OnButton2() 
{ 
    document.Form1.action = "Page2.java" 
    document.Form1.target = "_blank"; // Open in a new window 
    document.Form1.submit();    // Submit the page 
    return true; 
} 
--> 
</script> 
0

這是很奇怪的規定,但是要解決它,你將需要使用Ajax,否則你的表格將被張貼和頁面重新加載或新窗口中打開。最好的做法是將兩個帶有JavaScript功能的按鈕連接到它們。一旦按下按鈕,您甚至可以禁用一個按鈕,如果您需要特定的順序,則啓用第二個按鈕。

相關問題