File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes jsp include vs. link tag for including CSS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp include vs. link tag for including CSS" Watch "jsp include vs. link tag for including CSS" New topic

jsp include vs. link tag for including CSS

Mike Spenser

Joined: Jun 13, 2006
Posts: 21
I was wondering as to the pros and cons associated with including an external CSS file for a page.

So far the main thing I come up with is that <link...> is client-side relative whereas the the <jsp:include ...> is server-side relative in terms of the CSS file's location.

Is there anything more substational than this? performance hits associated with the jsp:include?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

The link tag will allow the browser to cache the CSS file. I've never seen a CCS page included on the server side and would consider that a poor practice.

[Asking smart questions] [About Bear] [Books by Bear]
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
I have done a combination of this before, using a <jsp:include> that would include a jsp, that renders the <link> tag for css.

the included file is a jsp, that contains the <link>tag to load either the external style sheet, or an inline style sheet, still within the <link> tag.

when i make a seperate file to put my css <link> tags, and other header type things that are common to all pages in my site, it saves me from having to cut-paste across all top level pages when something changes.
It is sort of like server side templating / organization of your conent. If you are used using the tiles framework, or similar, this jsp:include model fits nicely with the <tiles:insert> tags.

The motivation of using <jsp:include would render the contents of the seperate my_header.jsp is that the jsp becomes its own servlet, so the invocation is more like a function call, as opposed to if you <%@ include %> 'd it, then it would be verbatim inline with the calling servlet, which IMHO makes makes each file slighly unnecessarily larger and may at times (conspiracy theroy) take a little longer for the JSPs to compile (when you have hundreds)
I also prefer the <jsp:include over the <%@ include %> as I have noticed sometimes, on older (websphere) app servers do not always know to recompile a JSP when the <%@ include %>'d file is changed.

Error: Keyboard not attached. Press F1 to continue.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

I'd say that the decision to use an include directive vs. an include action should be made depending on whether it makes sense for a separate (sub)request to be made to the resource or not --not to get around a container issue.
I agree. Here's the link:
subject: jsp include vs. link tag for including CSS
It's not a secret anymore!