Posting a JSF page to Servlet

There are some use cases which requires posting data to a (legacy) Servlet for further processing and on success, redirect to a JSF page. This example illustrates a possible solution for similar scenarios - posting a JSF page to a Servlet. Basic idea is to invoke a custom javascript method on click of the button and submit the form to a customized 'action' target(Servlet).
<af:commandButton text="Post Me" id="cb1">
  <af:clientListener type="action" method="customPostHandler"/>
</af:commandButton>

<af:resource type="javascript">
  function customPostHandler(event) {
      var form = document.forms[0];
      form.action = '/some-context-root/sampleservlet';
      form.submit();
      event.cancel();
  }
</af:resource>
You can download the sample workspace from here.
[Runs with Oracle JDeveloper 11g R1 PS2]

How to run this sample?

Run the login.jspx page. This page is pre-populated with values for user name and password fields, leave the default values as is. Click on 'Post Me' button. This in turn posts data to 'SampleServlet' which processes the posted data, and on success, request is getting redirected to welcome.jspx

Comments

Post a Comment