Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling post method

 
triveni sangam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
If I click on a link, it will call a servlets get method. How can I make it call the post method of the servlet. In other words, if i click a href, it should call the doPost method instead doGet method of the servlet.
Thanks,
Javed.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
generally, i have seen things like these using the
form tags
< form ACTION=http://localhost:8080/servlet/FormExample METHOD=POST >
The METHOD=POST specifies that the doPost() method of the servlet
should be invoked.
one other way is that your doGet() method can call doPost().
While this maybe trivial and you may not be looking for this.
regds.
- satya
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I use doGet() invoke doPost().There are points to be considerred while doing this way.
1)If you are posting sensitive data and do not wish to appear in URl we use post.
2)If the form is very large that you are submitting then also it is advisable to use doPost().
3)Many times it happens that the browser gives "Method not supported error:doPost()".In this case you invoke doPost() through doGet().
What practice I always follow is doGet() invokes doPost().
This also depends on the requirement.
 
Sandeep Jain
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When u click on the href , the data that is send to the servlet is in the form of get that is the data goes with the url and not with the body u cannot invoke the doPost method with it .
However what u can do is in doget u can invoke the doPost mesthod and can carry on with the stuff that u want . But I dont think thats a good idea . As the data is not safe at all in this case.
ex:
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
//carry on the stuff u want
}

------------------
Sandeep Jain
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3)Many times it happens that the browser gives "Method not supported error:doPost()".
vaibhav:
just kinda curious, can you give me one scenario, where
you encountered this.....hopefully you can forgive my
ignorance as well.
Thanks.
- satya
 
Adam Priest
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want an href to post data to a server, you will probably need create a javascript onclick event to submit a form so when you click on the link, the event will cause a form submit. Of course the form must use the POST method.
<A href="myServlet" onClick='document.myForm.submit();'>Post data</A>
If you just want the servlet to take the GET but do a POST, it would be something like this:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}

Originally posted by triveni sangam:
HI,
If I click on a link, it will call a servlets get method. How can I make it call the post method of the servlet. In other words, if i click a href, it should call the doPost method instead doGet method of the servlet.
Thanks,
Javed.

 
Kevin Mukhar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:
[b]3)Many times it happens that the browser gives "Method not supported error:doPost()".
vaibhav:
just kinda curious, can you give me one scenario, where
you encountered this.....hopefully you can forgive my
ignorance as well.
Thanks.
- satya[/B]

This usually happens when the browser tries to POST to a servlet that does not have a doPost() method.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Oh yeah! I forgot that we don't have to implement both the
doGet() and doPost() methods.
Thanks Kevin.
- satya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic