Passing Parameters while switching Java Server Pages (JSP) - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Programming > PHP Development

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-06-2009, 03:00 PM   #1 (permalink)
Member
 
Join Date: Dec 2009
Posts: 123
Default Passing Parameters while switching Java Server Pages (JSP)

Since past few days, I was working on some Java Server Pages, which are linked with each other. Many times I have to include some JSPs in one JSP. While doing so, I need to call the variables defined in one JSP, in those JSP’s which are included one. I thus have to pass these parameters from one JSP to another at the time of Including those.

I searched for the solutions, and found many different ways of doing so.

For including another JSP into a JSP I used the following command in FirstJSP.jsp

<jsp:include page=”SecondJSP.jsp”/>

I searched for it and found many different ways of doing so.

1. JSP:Param

<jsp:include page=“SecondJSP.jsp” />

<jsparam name=“name” value=“Sulabh Jain” />

<jsparam name=“age” value=“24″ />

</jsp:include>

The above code will help in setting the values of param1 and param2 as value1 and value2

which can be called or retrieved in SecondJSP.jsp using,

<%

String myName= request.getParameter(“name”)

String myAge= request.getParameter(“age”)

%>

These can be printed in JSP using ‘equals to’ sign (=) with percentage tags,

My Name is <%=myName%>
My Age is <%=myAge%>

2. JSP:Forward

It also behaves similar to jsparam

<jsp: forward page=”SecondJSP.jsp”>
<jsp: param name=”name” value=”Amar Patel”/>
<jsp: param name=”age” value=”15″/>
</jsp: forward>

Limitations of above two methods :


This method of passing parameters can be used only for Static variables. For eg.
if we have a dynamic variable declared in JSP we cannot pass it using jsparam

Here it got little difficult,

Now I need to pass dynamic variables which I defined in the first JSP.
The third method provides solution to it.

3. request.setAttribute

Here we will use an object of javax.servlet.http.HttpServletRequest class named as request.
calling the public abstract void setAttribute(java.lang.String name, java.lang.Object o) method we can set the String name as an attribute.

<%

String name = “Sulabh Jain”;

request.setAttribute(“myName”, name);

%>

Now we can easily get in in SecondJSP as

<%

String nameInNewJSP = request.getAttribute(“myName”).toString();

%>

toString is used here to convert the object into string.
Weasley is offline   Reply With Quote
Sponsored Links
Old 12-06-2009, 03:02 PM   #2 (permalink)
Member
 
Join Date: Dec 2009
Posts: 123
Default Re: Passing Parameters while switching Java Server Pages (JSP)

Guys !! Please try running this code and let me know if all these methods works for you .......... Or if there are more options ...
Weasley is offline   Reply With Quote
Old 02-09-2010, 06:10 AM   #3 (permalink)
Junior Member
 
Join Date: Jan 2010
Posts: 94
Default Re: Passing Parameters while switching Java Server Pages (JSP)

thanks a lot.....
Robinson is offline   Reply With Quote
Old 02-20-2010, 01:38 PM   #4 (permalink)
Member
 
Join Date: Nov 2009
Posts: 101
Default Re: Passing Parameters while switching Java Server Pages (JSP)

wow thanks
masud7827 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 08:33 PM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting