Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can JSP use variable defined in another jsp ?

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic