Forwarding the Request from One Jsp to Another Jsp With All Request Parameters

Forwarding the request from one jsp to another jsp with all request parameters?

There are two ways to implement that:

  • output all parameters in hidden fields, and submit them in the 2nd request
  • store everything in the session

How to send values from one jsp page to other but redirect to some other page?

You can use a HttpSession object as a user session, or use the ServletContext object for sharing global application information. Then use methods getAttribute (String attb) and setAttribute (String attb, String value) for sharing information within JSPs. The issue when you use a request is that the domain of the request is constraining you to use this information only when you receive this request.

You can also use JavaBeans to share information within JSPs

EDIT: Have you included your Java code inside a scriptlet? Using <% your code here %>

How to pass the values from one jsp page to another jsp without submit button?

You just have to include following script.

<script language="javascript" type="text/javascript">  
var xmlHttp
function showState(str)
{
//if you want any text box value you can get it like below line.
//just make sure you have specified its "id" attribute
var name=document.getElementById("id_attr").value;
if (typeof XMLHttpRequest != "undefined")
{
xmlHttp= new XMLHttpRequest();
}
var url="forwardPage.jsp";
url +="?count1=" +str+"&count2="+name";
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("div_id").innerHTML=xmlHttp.responseText
}
}
</script>

So if you got the code, let me tell you, div_id will be id of div tag where you have to show your result.
By using this code, you are passing parameters to another page. Whatever the processing is done there will be reflected in div tag whose id is "div_id".
You can call showState(this.value) on "onChange" event of any control or "onClick" event of button not submit.
Further queries will be appreciated.

How can i get values from one jsp page to another jsp without redirecting to another page?

Have a look at jQuery AJAX, Its used to GET/POST data values to any page from Client to server. Without redirecting user to other page (changing URL in address-bar)

<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var value=$(this).val(); //it will get value of clicked button

$("#result_div").load('result.jsp?param='+value); //it will make ajax call to result.jsp by GET method
});
});
</script>

</head>
<body>
<input type="button" value="value1" />

<div id="result_div">
JSP Result will be loaded here.
</div>
</body>
</html>

In this example, I've use GET method and passed parameter named param that you'l have to read in result.jsp. Whatever result is given by result.jsp It will be loaded in #result_div

Useful Links:

https://api.jquery.com/load/

https://api.jquery.com/jQuery.get/

https://api.jquery.com/jQuery.post/

http://www.w3schools.com/jquery/jquery_ref_ajax.asp

https://api.jquery.com/jQuery.ajax/

Send value from 1 jsp to another jsp

Getting value from the request, you should be putting a value to a request. This code

request.getAttribute("testvalue");

is getting a value, but that code

request.setAttribute("testvalue", value);

is putting it.

This is because you want to avoid using the HTTP session.

how to pass values from one jsp to another jsp to update database

If the question is solely concerned passing parameter from one jsp to another.
You have got multiple ways. You can use something like this

 request.setAttribute("parameterName")

OR
You can use something like and tags. This way you can
pass the parameters. Even your code seems like passing the parameter, what kind
of exception you are getting?
Thanks,
Ben

How to redirect/forward request to another jsp page in servlet called by $.ajax() function

When you call a servlet via AJAX, you stay on the same page by definition, regardless of the headers sent by the server.

If you want to change the page, you must do it with javascript, in the success handler function for the $.ajax(..) call.

You can read the Location response header and set the window.location.href to that value. See here for other options.



Related Topics



Leave a reply



Submit