Solved

Access variable of parent jsp in included JSP

Posted on 2004-09-17
9
1,073 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

919 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

13 Experts available now in Live!

Get 1:1 Help Now