This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Can JSP use variable defined in another jsp ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Can JSP use variable defined in another jsp ?" Watch "Can JSP use variable defined in another jsp ?" New topic
Author

Can JSP use variable defined in another jsp ?

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Suppose I have a JSP file "First.jsp" like

***********
<% String name = "abc";
...
%>

...

<jsp:include page="Second.jsp" />
...
...
**********

Here is "Second.jsp":

*******************
...
user name = <%= name>
***********


Originally I have only one file "First.jsp", then I feel it is too long and logically part of it can be put into a separate jsp, "Second.jsp". That's why I use "include" in "First.jsp" to include the second file. But the second.jsp need to use the var defined in first.jsp. Do I have to redefine it in second.jsp ? What's the better way to make this work ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Too long? What is the point of the include? It doesn't really gain you anything.

If you want to reduce the complexity of the JSP, follow best practices and move the Java out of the JSP and into Java classes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Raj,
I think I remember using <%@include%> and using variables defined in another JSP. (It's been a while since I've put variables in a JSP now that I know it is bad practice.)

<%@include%> is a compile time construct which is why it works. <jsp:include /> happens at runtime which means the variables are not available since they are compiled separately.

I would use a separate jspf (jsp fragement) if the parts were logically separate or if something was being reused. Not just because the file is long. HTML tends to be long.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Originally posted by Jeanne Boyarsky:
Raj,
I think I remember using <%@include%> and using variables defined in another JSP. (It's been a while since I've put variables in a JSP now that I know it is bad practice.)

<%@include%> is a compile time construct which is why it works. <jsp:include /> happens at runtime which means the variables are not available since they are compiled separately.

I would use a separate jspf (jsp fragement) if the parts were logically separate or if something was being reused. Not just because the file is long. HTML tends to be long.



Hello Jeanne,

Even if I use <%@include%>, when it compiles "second.jsp", how can it compile ? It is a individual file and I didn't define the variable in the file.. don't I have to define it again in "second.jsp" to make it compile ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

When the include directive is used, the JSP with its included files is translated as a single unit.

But I still assert that includes are a poor way to break up code that is "too long".
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Originally posted by Bear Bibeault:
When the include directive is used, the JSP with its included files is translated as a single unit.

But I still assert that includes are a poor way to break up code that is "too long".


Thanks Bear. I can understand that "First.jsp" can be compiled well. But "Second.jsp" is a separate file. When the server compiles it, is it compiled separately ? if yes, how can the variables defind in "First.jsp" be transported to it ("First.jsp" includes "Second.jsp", but "Second.jsp" doesn't include the "First.jsp") ??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Originally posted by Raj Ohadi:
When the server compiles it, is it compiled separately ?
Please read my reply again.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Originally posted by Raj Ohadi:
When the server compiles it, is it compiled separately ?

No.

And I agree with Bear that this is not a good road to be going down.
 
Consider Paul's rocket mass heater.
 
subject: Can JSP use variable defined in another jsp ?