Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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 ]
 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic