views:

556

answers:

4

Hello,

I have a java servlet which get the form request from one webpage in domain A, and it would deal with the form, and would send the result in another form as a request to another webpage in domain B.

I'm wondering how to submit the form programmatically in Java servlet? I tried to use

javax.servlet.RequestDispatcher.forward(request, response)

but it doesn't work because it can only forward to a resource in the same domain.

+2  A: 

Try using Apache HttpClient for that

From the tutorial the code looks like:

HttpClient client = new HttpClient();
GetMethod method = new PostMethod(url);
int statusCode = client.executeMethod(method);
... etc

There are ton's of options to customize it.

OscarRyz
Is a good idea, but it really depends on what the original poster wants. Using Apache HttpClient makes the server serving the Servlet the client. Therefore, the actual client requesting the page on the Servlet didn't login.
lsiu
Thanks a lot. Will that forward to another page on domain B? I currently have no idea how the forward would work in my servlet in domain A.
Sapience
@Isui; Ohh I see, that makes sense. @Sapience: No , it won't. You may try submitting the form, and then in the response write an old school redirect ( HTTP-EQUIV=Refresh etc etc ) But as Isiu point out, your final client won't be recognized by the second server. That server should allow you to programatically start a remote session.
OscarRyz
+1  A: 

Try a javascript auto submit form returned by Servlet on domain A.

Servlet on domain A:

public void doPost(HttpServletRequest req, HttpServletResponse resp) {
  PrintWriter p = resp.getPrintWriter();
  p.print("<form id='f' action=[URL on domain B to login]><input type='secret' name='username' value='" + username+ "'/><input type='secret' name='password' value='" + password + "'/></form>");
  p.print("<script type='text/javascript'>document.getElementById('f').submit()");
}

This will not be the most elegant solution, but if you are looking for something more enterprisy, try SSO solution such as OpenSSO or CAS.

lsiu
Meaning Servlet domain A will return JS which has a auto submit in its onLoad which will submit it to domain B right Isiu? The only issue is what will the client (browser if is) will show?
Murali VP
Can set the the visibility through CSS of the form to hide the form.
lsiu
+1  A: 

You need to do an auto-post to the new domain. Just forward the request to a JSP like this,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body onload="document.forms[0].submit()">
<noscript>
<p>
<strong>Note:</strong> Since your browser does not support JavaScript,
you must press the Continue button once to proceed.
</p>
</noscript>

<jsp:useBean id="myBean" scope="request" class="example.myBean" />

<form action="<jsp:getProperty name="myBean" property="url"/>" method="post">
<div>
<input type="hidden" name="field1" value="<jsp:getProperty name="myBean" property="field1"/>"/>
...
</div>
<noscript>
<div>
<input type="submit" value="Continue"/>
</div>
</noscript>
</form>
</body>
</html>

The "myBean" contains the redirect URL and the field value needs to be posted to the other domain.

ZZ Coder
Thank you. This method should work on a browser on PC, but the problem is my client is a mobile phone, and its browser doesn't allow auto submit a form unless the user explicitly clicks on a button or link to trigger the submit.
Sapience
A: 

Big question is here: do you want to hand over the request to the other site and not worry about the response further? Or do you still want to have full control over the response so that you can present as if it is done under your own domain? This wasn't made clear in your topicstart.

If the first, then use the aforesuggested Javascript auto-submit-onload approach. If the second, then use the aforesuggested HttpClient suggestion (or if you know HTTP well enough, you could also use java.net.URLConnection).

BalusC