• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic include file question

 
Ulvi Cetin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to use dynamically generated file names in my <%@ include file="" %> directive.
I am using objects (such as database connection) in the included files which I created in my base JSP file. For this reason, I cannot use the jsp:include tag (since these objects are not defined in these files, the servlet doesn't compile).
Are there any ways of using these dynamic file names in this directive?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store the objects in request-scope properties, and use <jsp:include>.
[ August 22, 2003: Message edited by: Ron Newman ]
 
Faisal Khan
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulvi,
The issue here is to do with how the jsp:include and <%@ include file="" %> work. The former includes the file at request time therefore it has the specific variables and you can pass dynamic filenames. The latter is included at the translation times, thus it is not even aware of what the values are going to be so you can not include dynamic file names. There is a very nice explanation of this in SCWCD Study Kit.
As for being able to share the resources, there are better ways to achieve that such as placing the objects in request scope, session scope or application scope depending on the nature of the object and what you want to do with them.
IHTH
- FK
 
Ulvi Cetin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you suggest a way to pass the database connection object to the file using <jsp:include> tag? Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faisal gave you the clues. If the objects follow the bean pattern, you can place them in request scope on the parent page and use <jsp:useBean> to pick them up in the included pages.
If they are not bean-patterned, the included pages can pick them up using request.getAttribute() (which is what useBean will do behind the scenes anyways).
hth,
bear
P.S. I won't go into my usual tirade about the pitfalls of performing database actions from JSP pages; that's an architecture issue you can find out lots about with a search.
[ August 22, 2003: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!