Solved

Access variable of parent jsp in included JSP

Posted on 2004-09-17
9
1,149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:ramazanyich
ID: 12088541
why do you use exclamation mark in your first JSP ??
Just use <% String returnToAction="abc";%>
0
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

739 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