• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sendRedirect as POST

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Code Ranch,

I have a question on redirecting to a servlet. I have noticed that sendRedirect() method would always end up in the doGet method of the redirected page. how do I make sendRedirect point to doPost of the redirected page.

Page1Servlet


Page2Servlet


I have heard of some work around like creating a jsp or html form that would forward the parameter. But is there a way to do this in servlets.

Thanks,
Ian
 
Sheriff
Posts: 7146
1367
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, you cannot send a POST request using sendRedirect method. Why do you need doing that?
Moving to Servlets forum.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no direct way of sending a post request as the default method of request is get.also you cannot use response.sendredirect to send a post request.html form can be a solution to the problem.
 
Ian Cabigon
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Devaka and Archit.

Yeah, sendRedirect is really HTTP-GET. I still havent found another way to redirect to another page/servlet.

@Deveka: I need to do this because I want to pass a volume of parameters to the target(page to redirect) page. And making another jsp/html form would mean I have to touch JSP(View) side. I was thinking of handling everything in the Servlet(controller) side of the system. But if you have something in mind, I'd gladly like to hear from you.

Ian
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds as if both pages are part of the same web app, so why does it need to be a redirect? A server-side forward (using a RequestDispatcher) should work as well, and will also be faster.
 
archit thakur
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Lester Burnham
even, i agree with your point.

@Ian
if you want to use post over get just to hide your parameter name and value then request dispatcher is the best option for you as it uses get but does not allow the url to be seen in the browser. but if you have something else in your mind then you have to opt for jsp/html form.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is it possible for us to send a POST request using a link (<a href="#">Sent Post Request</a>) ?

Thanks,

Rahul
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An HREF link will always use GET (unless it uses JavaScript to cause a form submit that uses POST).
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lester Burnham wrote:An HREF link will always use GET (unless it uses JavaScript to cause a form submit that uses POST).



Yes, but then the form always uses POST method to submit, if we want to use the GET for some intention, then it will not be possible. Isn't it ?
 
archit thakur
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@rahul

Yes, but then the form always uses POST method to submit, if we want to use the GET for some intention, then it will not be possible. Isn't it ?


NO BUDDY,
form can use both <form action="" method="post"><input type="submit"></form> will send a post request and <form action="" method="get"><input type="submit"></form> will send a get request
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The underlying reason is that POST requests send paramters in the body of the request. Redirect can not recreate the body.

Getting the original POSTed parameters to the servlet at the new URL depends on where that destination is located - in the same application? In the same servlet container? In the same local network? ... etc.

Bill
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It seems I have the exact same requirement
.... I want to POST data tp an external servlet but do NOT want to use html <form>. I want to use server side code (like redirect)

I am integrating my web application with multiple external service providers (all providing the same type of service)

With most of these extenal systems, integration is pretty simple, standard and same
---> build a URL with required parameters (as provided in the service providers documentation)
........and in the code, simply invoke response.sendRedirect(toConstructedURL);

Only one entity does it in a different way (why? I am )

... they have provided me a sample HTML.
........The HTML is nothing but a <form> with a action, with method 'post'and multiple hidden fields within.
.........when the form is submitted, integration is achieved.

and so I want an server side code alternative to an html form submit. (I do not want to build a adapter for this one single thing)


thanks
Jeevan
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll either need to do it yourself with the classes in java.net, or use a 3rd-party tool such as HttpClient, to send the request.

java.net.URLConnection
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to redirect to remote URL with some parameters in it's POST request....
Any idea???
 
Bear Bibeault
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the reply right above your own.
 
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic