File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes one form, three actions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "one form, three actions?" Watch "one form, three actions?" New topic
Author

one form, three actions?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi,

This is really very basic question but I am not clear. This is my signIn.jsp file:



How can I send three different events from the same form?

In other words, There are three buttons at my page and I want different action on each click of each button. How could I do this??? or how can I know at server side that which button has been clicked by user???

I am sure there must be many ways of doing this, please tell me the simpler one and best one.

Thanks a lot.


[ November 26, 2005: Message edited by: Bear Bibeault ]
Oswald King
Greenhorn

Joined: Feb 11, 2005
Posts: 10
specify same name attribute for all the 3 submit buttons in your code


and then in your MainController :
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Or, use Javascript to change the action of the form upon clicking one of the submit buttons.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Bear Bibeault:
Or, use Javascript to change the action of the form upon clicking one of the submit buttons.


But all the requests are going first to MainController servlet so action is same in all the cases.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by rathi ji:
But all the requests are going first to MainController servlet so action is same in all the cases.


Sounds like you are using Front Controller Pattern. You can make the decision in your MainController on the basis of button value, as Oswald King already suggest said.

Think of implementing Command Pattern.
[ November 28, 2005: Message edited by: Adeel Ansari ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18124
    
    8

Or put the three buttons in three different forms.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Originally posted by babu amara:

so we have to write the code like below.

<INPUT type="submit" value="Sign In" action="A.html">

in the same way for below two also.
<INPUT type="submit" value="Sign Up" action="b.html">
<INPUT type="submit" value="Forgot Password" action="c.html">


Nonsense. There is no action attribute on the input tag.
[ November 28, 2005: Message edited by: Bear Bibeault ]
C Broussard
Greenhorn

Joined: Jun 18, 2004
Posts: 28
this is where the beauty of struts comes into play (or any standardized framework for that matter). Struts has a DispatchAction w/ an actionMethod defined in the struts action definition for this use case. why try to reinvent the wheel instead of using the wheel for leverage.

www.binaryfrost.com
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Originally posted by C Broussard:
this is where the beauty of struts comes into play (or any standardized framework for that matter). Struts has a DispatchAction w/ an actionMethod defined in the struts action definition for this use case. why try to reinvent the wheel instead of using the wheel for leverage.

www.binaryfrost.com


[highjack]
Because sometimes the wheel becomes a monster truck tire when a bicycle tire would have sufficed. Sometimes reinventing the wheel is a good thing.
[/highjack]


GenRocket - A Test Data Generation Platform
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Adopting a bloated behemoth like Struts in order to accomplish a simple switch would be a tad bit of overkill, in my opinion.
C Broussard
Greenhorn

Joined: Jun 18, 2004
Posts: 28
hey, don't shoot the messenger. j/k. I agree that this single example here doesn't provide enough of a business case to switch a bunch or adding a bunch of code around. here's some stuff to keep in mind. if he was using a standardized framework he most likely wouldn't be posting this question, he'd be posting a much more difficult question Ha Ha!. But seriously, this problem has already been addressed by someone else and solved. secondly, this is the signIn.jsp. sounds like he's doing more than a 20 action application, so it starts to warrant a standardized approach for long term maintainability, not some home-grown application... of course I could be completely off base.. I did join the party late.

www.binaryfrost.com

ps.. one other thing.. struts isn't bloated.. JSF is bloated
[ November 28, 2005: Message edited by: C Broussard ]
Babar Qadri
Ranch Hand

Joined: Oct 12, 2004
Posts: 51
Originally posted by C Broussard:
ps.. one other thing.. struts isn't bloated.. JSF is bloated


Hey, dont say a word to JSF.

I always did with my own fabricated framework. After JSF, I joined the JSF team and still working with JSF. Its good a bit immature but real good, simple, sensible. Its a component based framework and easily digestable. JSF is something really works and has a great future.

By the way, the discussion is going off topic.

I guess you dont know what she is doing. From her previous posts I got to know that she made her own mini web framework based on MVC architecture and going great with that, I hope. So, struts, or something else is meaningless for her right now.

I would also suggest the same, what Adeel and Oswald already suggested.
[ November 28, 2005: Message edited by: Babar Qadri ]

babar
sruthi adhuri
Greenhorn

Joined: Nov 19, 2005
Posts: 9
Try This....

<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function processForm(action)
{
if(action == "Sign In")
{
document.frm.action = "MainController";
}
else if(action == "Sign Up")
{
document.frm.action = "MainController1";
}
else if(action == "Forgot Password")
{
document.frm.action = "MainController2";
}
else
{
return;
}
document.frm.submit();

}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="frm" method="post">
username<INPUT type="text" name="username"/><br>
password <INPUT type="text" name="password"/><br>
<br>
<INPUT type="button" value="Sign In" onClic="processForm('Sign In')">
<INPUT type="button" value="Sign Up" onClic="processForm('Sign Up')">
<INPUT type="button" value="Forgot Password" onClic="processForm('Forgot Password')">
</FORM>
</BODY>
</HTML>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one form, three actions?
 
Similar Threads
My very own MVC framework...
why page not display
frecuent users with cookies
Can't get RequestDispatcher forward() method or response.sendRedirect() to work
Add a link to submit button