Solved

Access variable of parent jsp in included JSP

Posted on 2004-09-17
9
1,057 Views
Last Modified: 2013-11-18
I have a JSP (A.jsp) which contains a variable declared like this :

<%! String returnToAction="abc"; %>

Now I include another JSP in this by writing

<jsp:include page="../common/B.jsp"  flush="true"/>

In B.jsp I want to access the variable declared in A.jsp,the code is as follows

<a href="/tradeability/edit_payment_info?fromPaymentReturnTo=<%= returnToAction %>"> Edit Payment Information</a>

This does not compile and says that unresolved symbol returnToAction.

Can anybody suggest wats wrong with that.
0
Comment
Question by:vishrutsumit
9 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 12087045
<jsp:include page="../common/B.jsp"  flush="true"/>      
This dispalys the output  of B.jsp  
Can you use the  include directive ?
<%@  %>
0
 
LVL 27

Expert Comment

by:rrz
ID: 12087085
You could try
<%@ include file="../common/B.jsp" %>
0
 

Author Comment

by:vishrutsumit
ID: 12087776
even the include directive doesn't work
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 12088541
why do you use exclamation mark in your first JSP ??
Just use <% String returnToAction="abc";%>
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 21

Expert Comment

by:MogalManic
ID: 12091286
The problem is that the 'returnToAction' variable has a scope of the JSP page only.  When you leave the page in the @include directive, the variable does not exist.  The solution is to declare the bean in the REQUEST scope.  This can be done using the <jsp:useBean> tag.  Try the following:

<jsp:useBean id="returnToAction" class="java.lang.String" scope="request"/>
<%returnToAction="abc"%>
...
<jsp:include page="../common/B.jsp"  flush="true"/>


In the common/B.jsp:
<jsp:useBean id="returnToAction" class="java.lang.String" scope="request"/>
,,,
<a href="/tradeability/edit_payment_info?fromPaymentReturnTo=<%= returnToAction %>"> Edit Payment Information</a>

0
 

Author Comment

by:vishrutsumit
ID: 12112874
Logically this solutions should work.but it didn't,I had to use the param tags to pass the parameters.

Thanks all for your help.
0
 
LVL 10

Expert Comment

by:kupra1
ID: 12246214
I have used the same thing. It will work with the include directive.  It will not work with the include action tag as it will be included at the runtime and hence will throw the exception. Here is the example:
-------- (A.jsp)
<%@include file="B.jsp"%>
<html>
<head>
      <link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<%! String str = "Kupra"; %>
      <table border="1" width="95%" bgcolor = "#FFFFFF" cellpadding='0' cellspacing='0' bordercolor='#000000'>
            <tr>
                <td width="100%">
                               <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                 <tr>
                                          <td align="center" bgcolor="#FF0000"><b><span class="Menu"></b><a
                                          href="servicerprofile.do" target='_parent'>My Account</a></td>
                                          <td bgcolor="#FF0000">&nbsp;</td>
                                          <td bgcolor="#FF0000">&nbsp;</td>
                                          <td align="center" bgcolor="#FF0000"><span class="Menu"><a
                                          href="linkcutofftime.do" target='_parent'>My Blogs</a></td>
                                          <td bgcolor="#FF0000">&nbsp;</td>
                                          <td bgcolor="#FF0000">&nbsp;</td>                              
                                    </tr>
                              </table>
                        </td>
            </tr>
      </table>
</body>
</html>


----- and (B.jsp)
<html locale="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>Exception</title>
      <link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<table border="1" width="95%" bgcolor = "#FFFFFF" cellpadding='0' cellspacing='0' bordercolor='#000000'>
  <tr>
    <td width="100%">
            Hello How r u?<%=str%>
      </td>
  </tr>
</table>
</body>
</html>


Look at the fact that the variable str used in the B.jsp is declared in A.jsp only and is being successfully used.

Thanks


0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12531594
PAQed, with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now