Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect as POST

 
Ian Cabigon
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
archit thakur
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Nair
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jeevan Sunkersett
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • 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 :confused

... 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Nilesh Pat
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to redirect to remote URL with some parameters in it's POST request....
Any idea???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the reply right above your own.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic