GeeCON Prague 2014*
The moose likes JSP and the fly likes How do I access a value in an include from a parent jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "How do I access a value in an include from a parent jsp" Watch "How do I access a value in an include from a parent jsp" New topic
Author

How do I access a value in an include from a parent jsp

rastas biggs
Ranch Hand

Joined: Sep 24, 2004
Posts: 41
Hi there,

I have a value set in page i.e. page name.

In my header.jsp which I include in my page I want to display
data based on the page type.

How do I access the variable set in the page in the included .jsp file?

Cheers

rastas
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hi,

I am unable to visualise the situation which you have provided.

Either I am weak or the problem is not clearly defined

Regards
Rohit
[ April 17, 2005: Message edited by: Rohit Bhagwat ]
rastas biggs
Ranch Hand

Joined: Sep 24, 2004
Posts: 41
I have a value in a .jsp page

String page = "form"

I have an include.jsp in my form page.

I want to access the varaible "page" in the include.jsp

Ras
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
I had to go digging, but I knew this sounded familiar, turns out I've done this sort of thing before. I had a "header.jsp" that was included at the top of each of my JSPs, and it required that two variables of type String, named "category" and "action" had been declared in the calling page.

It worked for me, and being a couple of years removed from it, I started thinking about why it did work - seems like the "header.jsp" (the file to be included) should fail to get compiled properly....

I think the answer is in that I was doing a compile-time include:



as opposed to a run-time:



I think of the compile-time include like a C "#include", where the source is directly copied into the JSP, then the combined page compiled... so by the time the code inside the "header.jsp" is compiled, it's in the context of a page that has defined those variables.

Bottom line, here's my understanding:

If you're using the



method of including, you should just be able to reference those variables directly. If not, you'll have to set them into the request (or session, but to me, request sounds more like what you want), and retrieve them back out of the request in the included page.



.... ok... and I just found this looking through the docs on Sun's JSP Site :


A declaration has translation unit scope, so it is valid in the JSP page and any of its static include files. A static include file becomes part of the source of the JSP page and is any file included with an include directive or a static resource included with a <jsp:include> element. The scope of a declaration does not include dynamic resources included with <jsp:include>.


Hope this helps.

-- Jon Egan
Jayaraj Sekaran
Greenhorn

Joined: Apr 17, 2005
Posts: 5
hi rastas biggs,

Just search for jsp aram tag. Using this, you can pass the data as parameters to the included page and use it there.


SCJP 1.4<br />SCWCD 1.4<br />WSAD 5.0
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
<jsp:param> simply adds (or removes) request parameters that the included page can use. In the main jsp page:In the included file:Of course, this example is using EL. If you don't use EL, then you can replace the "${param.page}" with some scripting to retrieve the parameters.
[ April 18, 2005: Message edited by: Paul Bourdeaux ]

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
rastas biggs
Ranch Hand

Joined: Sep 24, 2004
Posts: 41
Is this JSP 1.2 compilant.

I assume rather than EL I can use the request object to get the params?

I'm just going to use the request object.

Ras
[ April 18, 2005: Message edited by: rastas biggs ]
 
GeeCON Prague 2014
 
subject: How do I access a value in an include from a parent jsp