简体   繁体   中英

How to send request from same JSP file to Multiple Servlets?

I want to navigate from same JSP to multiple Servlets.

Eg.

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

after clicking to button ( suppose button name is 'Register' ) request goes to 'actionFirst' Servlet.

there is anather Servlet 'actionSecond' and now i want to transfer the request to 'actionSecond' Servlet from same JSP file after clicking anather button ( suppose button name is 'Edit' ) from Javascript function. How can I acheive that?

You can try this.

 <!-- 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>

The script would look like

<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>

This is quite strange requirement, However to solve it, you will need to use Ajax, otherwise your form will be posted and page reloaded or new window opened. The best would be to have two buttons with javascript functions connected to them. You could even disable one button once it is pressed, and enable second if you need specific order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM