>>>Am I doing anything wrong?<<<
Yes!  :)

Below I outline two things. Firstly, why it's wrong, and secondly, what you
can do to achieve what you want to achieve. If you want to skip to the
solution (the description of why it's wrong is long winded), go down near
the bottom of my reply.

For the purposes of this discussion, let's use the following terms,
* JSP 1 - a JSP.
* JSP1Servlet - a name for the servlet class that JSP 1 is translated and
compiled into
* jsp1Servlet - an instance of the servlet.
* second.jsp - a JSP
* JSP2Servlet - a name for the servlet class that second.jsp is translated
and compiled into
* jsp2Servlet - an instance of the servlet.

When you're JSP 1 is translated into the class JSP1Servlet, JSP1Servlet
contains a class variable called "msg".

When jsp1Servlet comes to running the line of code corresponding to,
<jsp:include page="second.jsp" flush="true"/>
it actually calls jsp2Servlet as an independant, yet linked, servlet and
merges the output of jsp2Servlet with
the output of jsp1Servlet (although this merging of output is irrelevant as
far as your problem goes). (Another side note: In my opinion a more
descriptive label
for this tag would have been <jsp:run> or <jsp:call> or <jsp:merge> or
<jsp:includeoutput>. It's current form, <jsp:include>, while correct, is
slightly misleading relative to the directive <@ include>. The directive
reads in source code prior to translation - a genuine include in server page
parlance. The <jsp:include> tag causes a second jsp/servlet to be called,
and it's output merged, at runtime.).

Now let's talk about the cause of your problem. Your  problem comes back to
class basics. Code within jsp2Servlet cannot access the members of  other
classes without reference to the specific object containing those
instances - for instance, from jsp2Servlet you can't access "msg"; you would
have to use syntax like "jsp1Servlet.msg".

Now, the plot thickens because this won't work either. While the syntax is
correct, you have two problems:
1. A reference to jsp1Servlet is not automatically/readily available (I
could be wrong about that, but my next point makes this moot and not worth
the time looking up the API documentation to verify);
2. Even if you can retrieve a reference to jsp1Servlet, you don't actually
know (at design time) the classname being used by the servlet container for
JSP1Servlet (I've just called it JSP1Servlet for the purposes of this
discussion; this isn't
really the classname). Seeing you don't know the classname, you can't cast
back the reference to jsp1Servlet to an instance of JSP1Servlet (unless
runtime specification of a cast to be made is possible, but I've never
come across that. Has anyone ever seen a way of doing that?). Seeing you
can't tell the JVM the type of the reference, you can't ask the JVM to
access the "msg" member - the JVM would tell you that the reference doesn't
contain a member named "msg".

So what do you do?

The solution is simple. In JSP 1, replace this,
<%! String msg ="Hello"; %>
with this,
<% request.setAtrribute("msg","Hello"); %>

In second.jsp, replace this,
<%= msg %>
with this,
<%= request.getAtrribute("msg") %>

Rob

----- Original Message -----
From: "Alireza Nahavandi" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, March 19, 2001 6:49 AM
Subject: include file


> Hi All,
> I guess we had this discussion a while ago. For passing parameter to
include
> files somebody suggested this way :
>
> ---JSP 1 ----
>
>   <%! String msg ="Hello"; %>
>
>   <jsp:include page="second.jsp" flush="true"/>
>
> --- second.jsp ----
>
>    <%= msg %>
>
> ---------
>
> It still complaining that variable "msg" does not exist. Am I doing
anything
> wrong?
>
> Thank you,
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to