Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to invoke servlet on click of non-submit button?  RSS feed

 
RabiDas Sharma
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I'm doing Result Management module for my online test taking system(a web based apps)

In the home page of it there are 4 buttons
1>create result
2>view result
3>update result
4>delete result

I want that on click of create result button one servlet's(servlet1) service method will be invoked
and on click of view result it should go to another servlet's(servlet2) service method will be invoked
and on click of update result it should go to another servlet's(servlet3) service method will be invoked
and on click of delete result it should go to another servlet's(servlet2) service method will be invoked.


shall i have to create 1 form for each action??

Is there any other way to do the same??
instead of creating 4 forms can we make single form and on click of each button
can we make action(i.e, call a sevlet's service())

thanks
with regards
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Why is that 4 different actions instead of a single one with a hidden parameter indicating the operation to be done on the server?

2) If the method is not specified, GET is the default - some of these actions (3 of 4, actually) sound like they should use POST instead.

3) You should never override (or call) a servlet's service method, always doGet or doPost as appropriate (and the two should never do the same thing, that would violate the HTTP specification).
 
RabiDas Sharma
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,
i'm sorry to ask you again.
could you please give me code snippet for what you said.

1) Why is that 4 different actions instead of a single one with a hidden parameter indicating the operation to be done on the server?


<form method="doGet" action="servlet1"//how to pass parameter>
doubt: 1.how to pass parameter
2.I'm using doGet for CRUD operation after going doGet() in servlet I will redirect servlet to different jsp pages using parameters
if(parameter="create")
i will redirect to create.jsp(where it will take test id as input from dynamically populated drop down)
else if(parameter="view")
i will redirect to view.jsp((where it will take test id as input from dynamically populated drop down)
like wise update and delete..

will it be alright?
please rectify me if i'm wrong


thanks
with regards
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it will not be alright as long as you use GET. And be aware that the HTTP method (which you would use for the action attribute) is called "GET". "doGet" is a method of the servlet API - it has no place in an HTML form.

It could look something like this:

Note that it uses POST for what looks like operations that change state on the server, and GET for the one that looks like it does not. (I may be misinterpreting the intentions behind these, so that may have to be adjusted.) So you would have to override both doGet and doPost, and take the appropriate actions in them as determined by the "action" parameter.
 
RabiDas Sharma
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ulf

please help me in my another doubt.

if i call doPost() 3 times for creation ,updation and deletion then how can i distinguish between them for different set of things to happen each time.
shall i use a static variable in servlet which will get incremented on each invokation of doPost()



with regards
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rabidas,

If you use the form Ulf posted and a debugger you can look at the parameters that are returned.

Something like


will tell you which button was pressed and you could use a switch statement to process appropriately.

Note that I did not compile that line of code so it might not work but it's close.

Joe
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RabiDas Sharma wrote:
if i call doPost() 3 times for creation ,updation and deletion then how can i distinguish between them for different set of things to happen each time.
shall i use a static variable in servlet which will get incremented on each invokation of doPost()


I think Ulf has used for the same purpose.

use
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RabiDas Sharma wrote:
if i call doPost() 3 times for creation ,updation and deletion then how can i distinguish between them for different set of things to happen each time.
shall i use a static variable in servlet which will get incremented on each invokation of doPost()



In REST terms, you would use the HTTP methods PUT POST and DELETE which the service method would use to select doPut, doPost or doDelete methods in your servlet to create, modify and delete. However I suspect browsers are only happy with POST and GET, therefore put a hidden variable in your form like Ulf suggests.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never, under any circumstances*, use read-write static or instance variables in a servlet. You will, not might, will introduce threading issues that will reek holy hell on your application.














* OK, there may be corner cases, but for the most part "never" is close enough!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!