Sriram
Sriram

Reputation: 65

Sending Request and getting response while page loads in JSP

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

Upvotes: 0

Views: 1382

Answers (1)

Vinoth Krishnan
Vinoth Krishnan

Reputation: 2949

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.

Upvotes: 1

Related Questions