• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Can JSP use variable defined in another jsp ?

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67694
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic