0

I am pretty much new to JSP and wanted to understand the possibility of doing the below functionality in JSP.

  1. Submit a form with a text box to a JSP page
  2. When the JSP page loads, it should use the value from textbox and in turn should send another HTTP request to another URL and get back HTML response.
  3. This response should be rendered on the same JSP page.

Would it be possible to do this in plain JSP without using Servlets? Any suggestions would greatly help.

Thanks in advance

Sriram
  • 55
  • 1
  • 9

1 Answers1

1

Use jQuery ajax to load the content when you loading the page.

In the JSP page we assume you've got the text box value,

$( document ).ready(function() {

   var text_box_value = $("#my_text_id").val();
   $.ajax({
       method: "POST",
       url: "Your_second_jsp",
       crossDomain: true,
       data: { data: text_box_value}
   })
   .done(function( response ) {
       $("#your_div_id").html(response)
   });
});

And populate your response in html (here i'm using div)

<div id="your_div_id">
     <!-- Your response will be displayed here. -->
</div>

which will help you to get the HTML.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • Thanks for the Suggestion Vinoth. Would it be possible to have the logic handled completely on JSP? AJAX can be used. But we may face CORS issue in sending the AJAX requests, since the domains are going to be different. – Sriram Mar 08 '16 at 07:40
  • Add `crossOrigin: true,` parameter in your request for cross origin issue. – Vinoth Krishnan Mar 08 '16 at 07:43
  • Check [link1](http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working) and [link2](http://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis) for more details. – Vinoth Krishnan Mar 08 '16 at 08:05