Question

I have two jsp pages. The first one with "Hello World" text message. The second JSP page with "Hello JSP" text message. Now in my third JSP page I have two buttons button1 and button 2. Can someone suggest some code snippet which I can use to call the first JSP page on click of the button 1 and **on click of button 2 , it should call the second JSP page?

On click of the first button only the first jsp page value should be displayed i.e Hello World and on click of the second button only second page value should be displayed i.e Hello JSP.

Kindly suggest the approach to achieve this.

Kind regards

Was it helpful?

Solution 2

JavaScript using AJAX

<script>
function loadXMLDoc(page)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",page,true);
xmlhttp.send();
}
</script>

Code For Buttons

<button type=button onclick='loadXMLDoc("page_one.jsp")'>Button 1</button>
<button type=button onclick='loadXMLDoc("page_two.jsp")'>Button 2</button>

And inside body

<div id="myDiv"></div>

OTHER TIPS

Just put some javascript that sets the window location to the page you want to display as the script of the button

<input type="button" 
       value="Button 1" 
       onclick="window.location.assign('HelloWorld.jsp')">
<input type="button" 
       value="Button 2" 
       onclick="window.location.assign('HelloWorldJSP.jsp')">

Put the correct addresses in there for your JSP pages. This will cause the browser to reload from the address of the JSP page, and that JSP will 'fill the browser' with its contents.

It does not make a lot of sense to "call" a JSP page. Are you trying to submit a different set of information based on which button you click (I think so)? And if you submit different information, what do you want to do with the result?

The simplest thing - have two forms that get submitted to different URLs. Clicking on button 1 should submit it to the URL of first JSP, clicking on button 2 should submit it to the URL of the second JSP. Or these could be simple GET links. If the three JSPs, a.jsp, button1.jsp, and button2.jsp are in the same (and accessible through direct web URL) directory , a.jsp could be -

Click for Hello World Click for Hello JSP

Content of button1.jsp and button2.jsp is trivial.

Keep in mind that in general, you should not be submitting directly to a JSP URL. May I also suggest that you spend some time reading up on how JSP, servlets and HTTP work in general and in conjunction with each other?

Always try your practices to forward via the servlets, and below is one of the implementation. Set both buttons as a submit type. Use hidden textbox for storing the clicked id/name of button inside of the form.

$("#button1").click(function() {
 var buttonId = $("#buttonId").val('button1');
 return true;
});

$("#button2").click(function() {
 var buttonId = $("#buttonId").val('button2');
 return true;
});

Submit the form and get the value of buttonId in your servlet like this,

String clickedButton = request.getParameter("hidden_name");

finally forward the page to whatever you like this,

if(clickedButton.equals("button1")){
// do your stuff
 }else if(clickedButton.equals("button2")){
// do your stuff
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top