Two Laptop Bag*
The moose likes Servlets and the fly likes Forward to page expecting POST request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forward to page expecting POST request" Watch "Forward to page expecting POST request" New topic
Author

Forward to page expecting POST request

joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
Hi,
I have a servlet which uses a requestdespatcher to forward the user to various pages according to the action they have chosen in a webapp.

In one case I want to forward them to the login page of a php blog software package. I want to fill the username field on the login page with their username, which I have in the servlet.

I can see how to do this - I need to pass the username as a [_POST] variable for the php page to read. I can do this from an html web form, but how do I do it from a java servlet?

Hope that made sense! Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Please take the time to choose the appropriate forum for your posts. We have a forum entirely devoted to servlets. This post has been moved there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

I'm not very familiar with PHP but does it really make a distinction between reuqest parameters passed in a post vs. those passed via a get?

If not, then a redirect, rather than a forward will let you place the request parameter on the redirect URL.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

I solved a similar problem by creation of a proxy servlet which issued POST request to PHP page and returned result back. It isn't very elegant solution, though.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
Hi Bear,
Yes, the distinction is made between GET and POST, so passing the variable on the end of the url doesn't work.

D Rog, I didn't understand your solution. I have found examples of sending POST requests from JAVA and handling the return, but (perhaps I'm confused here) I actually want to direct the user to the page expecting the POST request.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Ugly, but easy solution:

Have the servlet forward to a JSP page that contains a form with a hidden parameter representing the POST data, a method of POST, and an action that addresses the PHP page. Javascript in the onload handler causes the form to submit.
joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
That would certainly do it. Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Would it work to change your servlet so that it requires POST requests?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Paul Clapham:
Would it work to change your servlet so that it requires POST requests?


I don't think so in this case. The parameter isn't on the original request, and so a forward to the PHP page won't contain the parameter. That's why I though about using a redireect. But that will initiate a GET.

Sigh.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Originally posted by joseph corner:
Hi Bear,
Yes, the distinction is made between GET and POST, so passing the variable on the end of the url doesn't work.

D Rog, I didn't understand your solution. I have found examples of sending POST requests from JAVA and handling the return, but (perhaps I'm confused here) I actually want to direct the user to the page expecting the POST request.

This solution is worser than Bear proposed but similar, your servlet just returnr to browser whatever PHP call returned to it. An intermediate page looks better with one drawback, like JS not avaialable (certainly not your case)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Initiating the POST under Java control and returning the body of the response as the response of the servlet would indeed be problematic. The URL sitting at the browser would be that of the servlet and not of the PHP page. The PHP page is unlikely to operate correctly under this situation unless is was specifically coded (with full absolute URLs, for example) with this situation in mind.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

I do not see why coding few lines of code is problematic [edit]. Any forward makes browser think that response was taken at original URL (it's matter of all forwarding). Again I consider your approach is better unless JS can be disabled or not supported.
[ February 22, 2007: Message edited by: Ben Souther ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

[edit]

What you are not seeing is that the PHP is not under the control of the servlet author and he can't assume that the PHP page was written to have no relative references. In fact, I'd be willing to bet that there's a 100% chance that it's not. Do you write your pages with no relative references?
[ February 22, 2007: Message edited by: Ben Souther ]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

[edit]

You can use relative references, but you or somebody else here teached me to put

to avoid surprises. Again, look in the title of the thread, see word Forwarding? Your solution is redirecting and I consider yours better than mine, why aren't you still satisfied?

[ February 21, 2007: Message edited by: D Rog ]
[ February 22, 2007: Message edited by: Ben Souther ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

[edit]
You can use relative references, but you or somebody else here teached me to put


The point is that the original poster cannot change the PHP to put the base tag in or make any other changes.

why aren't you still satisfied?


I'm completely satisfied. I'm merely pointing out, to anyone that might be reading this topic at some later point, that a solution using an internal fetch and a forward cannot work in this situation for the reasons I stated.

[ February 21, 2007: Message edited by: Bear Bibeault ]
[ February 22, 2007: Message edited by: Ben Souther ]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

[edit]

First, why are you sure that PHP doesn't use <BASE HREF> already?
Second, you can add <BASE HREF> in response taken from PHP. I know you are trying to tell it isn't a trivial task, however it's quite possible. We are trying to argue without knowledge of all conditions of the problem, so without this knowledge both solutions are equal.
[ February 22, 2007: Message edited by: Ben Souther ]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

[edit]

Here is one more thing a developer should aware, it's a security. an intermediate page keeping user credentials can be cached by browser and then taken by a hacker.
[ February 22, 2007: Message edited by: Ben Souther ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by D Rog:
I know you are trying to tell it isn't a trivial task, however it's quite possible.


No, I'm not saying it's not trivial. I am saying that it is not possible. Again, unless the PHP page is written to be able to be displayed using a foreign URL -- something I'd wager is a 0% probability -- it just plain won't work.

First, why are you sure that PHP doesn't use <BASE HREF> already?


Whether the PHP page contains a base tag is moot. How could it possibly contain the correct href for another site that it knows nothing about?

Second, you can add <BASE HREF> in response taken from PHP.


That will not help in the least. The response created after the PHP is submitted has nothing to do with how the browser will interpret the URLs within the PHP page.

I'm sorry that my pointing out the problems with your scheme strikes you as unfriendly; but that doesn't change its non-viability.

The only way that such a scheme would work was if the PHP page was changable. And in that case, then it could be re-written to remove to POST problem to begin with and none of this would be germaine.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First, let's not drift from the topic at hand.
I've removed anything that wasn't related to the servlet question asked by the original poster.

Originally posted by joseph corner:
Hi,
I have a servlet which uses a requestdespatcher to forward the user to various pages according to the action they have chosen in a webapp.

In one case I want to forward them to the login page of a php blog software package. I want to fill the username field on the login page with their username, which I have in the servlet.

I can see how to do this - I need to pass the username as a [_POST] variable for the php page to read. I can do this from an html web form, but how do I do it from a java servlet?



Joseph,
How are you forwarding to a PHP with a request dispatcher?
Putting the post/get issue aside, have you been able to forward to a PHP page and have it execute?
It is my understanding that a requestDispacher.forward can serve up a static resource or execute another servlet. I wasn't aware of any way to forward to a PHP, Perl, or other script.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Originally posted by Bear Bibeault:


That will not help in the least. The response created after the PHP is submitted has nothing to do with how the browser will interpret the URLs within the PHP page.

I'm sorry that my pointing out the problems with your scheme strikes you as unfriendly; but that doesn't change its non-viability.

The only way that such a scheme would work was if the PHP page was changable. And in that case, then it could be re-written to remove to POST problem to begin with and none of this would be germaine.

Ok, proxy servers are very popular and certainly work well for any type of service providers, as servlet, PHP, CGI and so on. So, PHP does a simple work, parses parameters, do some business and return result back. It can be HTML, XML, or some other content type. A task of our servlet just return this data back to browser. It plays a clear proxy. Now it can be a bit proactive and to a simple response filtering, for example adding <BASE HREF> unless it's already there. I'm writing so confident, because I already developed some proactive proxy long time ago. You can aceess any web site regardless how it was implemented and get a little more feature, for example few custom buttons, extra menus and so on. So, believe me although the task looks impossible from your point view, it's real possible in real practice.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

The subject of proxy servers is beyond what we're talking about here, and you continually seem to want to talk about the response of the PHP page which is not the issue.

If you want to discuss proxy serving, please start a new thread to discuss that subject.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

It isn't a proxy server discussion, it's a proxy role of a servlet for solving a particular engineering task. Sorry, my native language isn't English, so simple you do not understand my point. We can close this discussion since OP is already OK with a solution you proposed. Are you central states located?
 
Consider Paul's rocket mass heater.
 
subject: Forward to page expecting POST request
 
Similar Threads
Forwarding an attribute from 1 jsp to another?
Unix Authentication by using servlet
how to keep input when back to this page?
How to Retrieve a Servlet Session variable in PHP
how to let Java login tomcat form authorization