How to submit a form programmatically in Java servlet?

11,765

Solution 1

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.

Solution 2

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.

Solution 3

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">
<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.

Share:
11,765
Sapience
Author by

Sapience

Updated on June 04, 2022

Comments

  • Sapience
    Sapience almost 2 years

    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.

  • lsiu
    lsiu over 14 years
    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.
  • Sapience
    Sapience over 14 years
    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.
  • OscarRyz
    OscarRyz over 14 years
    @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.
  • Murali VP
    Murali VP over 14 years
    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?
  • lsiu
    lsiu over 14 years
    Can set the the visibility through CSS of the form to hide the form.
  • Sapience
    Sapience over 14 years
    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.