wood burning stoves*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 60810
    
  65

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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: 60810
    
  65

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: 60810
    
  65

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

Joined: May 26, 2003
Posts: 30138
    
150

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can JSP use variable defined in another jsp ?
 
Similar Threads
Passing values to JSp with href
"jsp:usebean" question
request.getHeader("Referer")
How to display two different jsp pages in one consolidated jsp page
multiple forms in JSP