Meaningless Drivel is fun!*
The moose likes JSP and the fly likes Sharing a scripting variable across JSPs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Sharing a scripting variable across JSPs" Watch "Sharing a scripting variable across JSPs" New topic
Author

Sharing a scripting variable across JSPs

swarupa patil
Ranch Hand

Joined: Feb 22, 2006
Posts: 62
Hi All,
I am using
One.jsp

Hello
<% String msg="H R U?"%>
Bye

and I want to use above String variable of one.jsp in another
jsp or servlet file ? Can I use variable ?

Please give me some code snippet
[ February 24, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

No. Not any more than you could use a variable delared in a method of one Java class in another Java class.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
Originally posted by Bear Bibeault:
No. Not any more than you could use a variable delared in a method of one Java class in another Java class.


Not impossible at all if you used the static include directive. I don't like it, though. A web application with a lot of this kind of code could be a nightmare for maintenance.



You can access the reference msg from the included jsp. The include directive is position-dependent. If you move the directive up before the scriptlet, it will not compile. Because the raw content of the included page is inserted at page translation time.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

What you describe can in no way be considered sharing the variable across JSPs. The included file, whose conventional suffix should be .jspf rather than .jsp, is not a separate JSP, but a fragment that becomes part of the main JSP.

That's like saying you can share a variable across C modules by using the #include directive.
[ February 24, 2006: Message edited by: Bear Bibeault ]
S bitz
Ranch Hand

Joined: Jan 20, 2006
Posts: 80
I use sessions to pass the variables from one jsp/servlet to another. You want to do it without using session attributes?

[ February 24, 2006: Message edited by: S bitz ]
[ February 24, 2006: Message edited by: S bitz ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I would say Yes, you can use it after setting it to some scope.
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
Originally posted by Bear Bibeault:
What you describe can in no way be considered sharing the variable across JSPs. The included file, whose conventional suffix should be .jspf rather than .jsp, is not a separate JSP, but a fragment that becomes part of the main JSP.

That's like saying you can share a variable across C modules by using the #include directive.


I mentioned the include directive inserts the included page into the main page at translation-time for compilation as a single unit, meaning that it is a fragment of the resulting unit. Didn't say about sharing a variable across different classes.

Maybe it is my English... Probably need to spend more time on English study than on technical stuff.

I was talking about something like the following:

In main.jsp


and in fragment.jsp


I was just giving an example that you can declare a variable in one jsp and use it in another jsp even though they are actually compiled as one single class.

Have a good weekend!!!
[ February 24, 2006: Message edited by: Heonkoo Lee ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

The summary is: no, you cannot share the scripting variable across JSPs,

You can share the string object that is referenced by the scripting variable by creating a scoped variable in session or application scope that also references the string. But the scripting variable itself cannot be shared.
[ February 24, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Originally posted by Heonkoo Lee:
I was just giving an example that you can declare a variable in one jsp and use it in another jsp


My point is that you did not give an example of using a variable in more than one JSP. When you perform a static include there is only one JSP. So there's nothing else to share with,

Just because you erroneously called the included fragment a JSP doesn't make it one.
[ February 24, 2006: Message edited by: Bear Bibeault ]
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
Originally posted by Bear Bibeault:


My point is that you did not give an example of using a variable in more than one JSP. When you perform a static include there is only one JSP. So there's nothing else to share with,

Just because you erroneously called the included fragment a JSP doesn't make it one.

[ February 24, 2006: Message edited by: Bear Bibeault ]


I did not erroneously call the fragment a JSP. You can statically include a JSP file or HTML file into another JSP file. File name extension of the statically included file does not matter. The including and included JSPs are two separate .jsp files physically existing in the file system. They are compiled into a single class.

When the including jsp (main.jsp) is invoked the first time, the raw content of the including and included jsp files are combined/converted into one main_jsp.java and then compiled as main_jsp.class. And it displays the message string updated by fragment.jsp.

But, the fragment.jsp itself cannot be compiled directly if it is using undeclared variable. If you try to invoke the fragment.jsp on a browser, you will see something like below:

--------------------------------
org.apache.jasper.JasperException: Unable to compile class for JSP

message cannot be resolved
--------------------------------

I do not mean that it is possible to share a local variable between two different classes independently compilable. But talking about the static inclusion case. You can even name the included fragment file as foo.txt.

This is all I am saying. Hope I am not making anybody confused.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

You could possibly be confusing people, especially beginners, if you keep insisting that the fragment included via a directive is a JSP file. It is not. A JSP file must be a syntactically complete unit. For example, it you were to include a page via the include action (as opposed to the directive), the referenced page must be a complete and syntactically correct JSP page. When using the include directive, the included file does not need to be a syntactically correct JSP page.

So calling the included file a JSP is incorrect and misleading. It's just a file with a fragment of text in it that becomes part of a JSP.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

You could possibly be confusing people, especially beginners, if you keep insisting that the fragment included via a directive is a JSP file. It is not. A JSP file must be a syntactically complete unit. For example, it you were to include a page via the include action (as opposed to the directive), the referenced page must be a complete and syntactically correct JSP page. When using the include directive, the included file does not need to be a syntactically correct JSP page.

So calling the included file a JSP is incorrect and misleading. It's just a file with a fragment of text in it that becomes part of a JSP.
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
Originally posted by Bear Bibeault:
You could possibly be confusing people, especially beginners, if you keep insisting that the fragment included via a directive is a JSP file. It is not. A JSP file must be a syntactically complete unit. For example, it you were to include a page via the include action (as opposed to the directive), the referenced page must be a complete and syntactically correct JSP page. When using the include directive, the included file does not need to be a syntactically correct JSP page.

It is like you saying that somebody is looking at Java code printout that spans 2 pages, and thinking it is wrong because undeclared variable is being used on the 2nd page, which is declared on the 1st page.


So calling the included file a JSP is incorrect and misleading. It's just a file with a fragment of text in it that becomes part of a JSP.

Did I ever say opposite to this? But, I as well as some books and references can still call the included file a JSP whether it is dependent on any JSP or independently compilable unit. 'So calling the included file a JSP is incorrect' is merely just your opinion.

To help make it more clearer, the following is from JSP v1.1 Syntax Refrerence: (old version but still valid)



Description:

The <%@ include %> directive inserts a file of text or code in a JSP file at
translation time, when the JSP file is compiled. When you use the <%@include %> directive, the include process is static. A static include means that the text of the included file is added to the JSP file. The included file can be a JSP file, HTML file, or text file. If the included file is a JSP file, its JSP elements are translated and included (along with any other text) in the JSP file. Once the included file is translated and included, the translation process resumes with the next line of the including JSP file.
The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language. Be careful that the included file does not contain <html>, </html>, <body>, or </body> tags. Because the entire content of the included file is added to the including JSP file, these tags would conflict with the same tags in the including JSP file, causing an error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sharing a scripting variable across JSPs
 
Similar Threads
Handling response.redirect errors in JSP page
jsp
Regarding sendRedirect
syntax and error in ncf log
Cookie Problem