Solved

Access variable of parent jsp in included JSP

Posted on 2004-09-17
9
1,179 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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:
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.

688 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