wood burning stoves 2.0*
The moose likes Servlets and the fly likes form action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "form action" Watch "form action" New topic
Author

form action

Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
Hi, I have a rather general question: when we have two input type in a html page(ie. two submit buttons), one is submit, the other is preview. How can the servlet tell which action should be taken based on the input type?
Thanks!
Brett Knapik
Ranch Hand

Joined: Oct 15, 2000
Posts: 255
most likely you would have 2 servlets, do a switch statement to get the type of submit it was (preview or post) or if they have 2 different actions you could have doPost or doGet
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
can someone throw some more light on this topic.
1)How would you configure the same on your HTML page and are there any changes that have to be made to the two servlets? How are the two servlets distinguished???
sagar
Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
Yes, and basically, I think it comes down to how do you extract Input Type's value to servlet?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I use this technique in my applications.
We can have 1..N buttons in 1..N html forms. I always make the name of those buttons SAME as 'RequestType' and their values to be different, like 'Submit' or 'Preview' or 'Update' or 'Delete'
and these buttons are inside a form whose ACTION can call 'MyServlet'. In MyServlet , we have to do the recognizion work. A sample from my code is in doGet(..) of MyServlet is

I had written this for easily scalable and can work even if the cookies are disabled in the browser , and DOMAIN and LEAF are got from configurable params etc. THis is the reason, I made this as .jsp file and the looonnng <FORM> line.
For testing purposes, this line will do
<form name="form1" action="/servlet/MyServlet" action="POST">
regds
maha anna

[This message has been edited by maha anna (edited December 30, 2000).]
Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
maha, can you just cal aHttpServletRequest.getParameter("submit"), and get the value of the input type...? What is the purpose of the ID, and how will that be used in servlet?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Bob,
ID is redundent. Not needed. Generally, if we use a Web authoring tool like HomeSite or NetFusion, those s/w keep track the total comps in a html form with this name ID.
I used NetFusion, so is the name ID came in those button fields. Not necessary for the server side, and ID's will not be sbmitted to servlets when we make a submit of this form.
For most of HTML controls there are generally 3 attributes.
1.Type 2.Name 3.Value
Example
<input TYPE=SUBMIT NAME=FirstButton Value=FirstValue>
<input TYPE=SUBMIT NAME=SecondButton Value=SecondValue>
Since here both buttons (First and Second) are of Submit types, when one of them is clicked by user, the corresponding resource in ACTION attribute of <FORM> within which this submit buttons exist will be invoked. There are 2 types of METHOD in <FORM> . 1.GET and 2. POST.
In both cases, when a form is submitted, we can get the information about which button is clicked and its value in the servelt by means of request.getParameter("FirstButton") OR request.getParameter("SecondButton").
The returnd String will be "FirstValue" and "SecondValue" respectively. This is first method, where both submit buttons are having different names and different values. For this case the, the servlet MUST KNOW the EXACT NAME of the buttons like "FirstButton" or "SecondButton" to retrive the values.
Another method is just have the SAME name for all Submit buttons, like 'RequestType' and their values DIFFERENT. The advantage of this 2nd method is , we need not hard-code the button names in Servlets. Just ask of request.getParameter("RequestType") and compare the returned String values and take the action accordingly.
So the 2nd case is more flexible, in the sense , the HTML autjors are free to add more buttons and only contract bet the servlet developer and HTML author is the names of those buttons should be 'RequestType' and the VALUE can be same or different.
To visualize the 2nd case, if a user is viewing an Employee's details and this page can have 'DELETE' or 'MODIFY' or 'NEW' buttons at the bottom of the page , all are of SUBMIT types and all operations will be handled by EmployeesServlet. Just have them all SAME names and their values different and at the server just use request.getParameter("RequestType") and take action accordingly.
So bottomline request.getParameter("CONTROL_NAME") will retrun CONTROL_VALUE. In our case CONTROL_NAME ="FirstButton" and CONTROL_VALUE = "FirstValue".
We can do either way. The 2nd one makes servlet developer's life a little bit easier.
regds
maha anna

[This message has been edited by maha anna (edited December 30, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: form action