4

I have common css and js files that i include in every jsp file.

So what's the best practice to include them in every page ?

I used to use <%@ include file="header.jsp" %> but I'm wondering if this is the best and clean way to proceed.

Hayi
  • 6,972
  • 26
  • 80
  • 139

5 Answers5

3

You can try this without any framework, in your web.xml:

<jsp-config>
   <jsp-property-group>
     <display-name>Display</display-name>
     <url-pattern>*.jsp</url-pattern>
     <el-ignored>false</el-ignored>
     <scripting-invalid>false</scripting-invalid>
     <is-xml>false</is-xml>
     <include-prelude>/template/header.jsp</include-prelude><!-- header -->
     <include-coda>/template/footer.jsp</include-coda><!-- footer -->
   </jsp-property-group>
 </jsp-config>

See more here

  • I don't think it's a good solution for 3 reasons. First the behaviour depends on your servlet container / application server, please find a single documentation not related to a specific editor (you linked a weblogic doc). Second, it's not flexible at all. Third, if you use Servlet 3.0 you might no have web.xml at all! – alain.janinm Nov 20 '14 at 13:39
  • @alain.janinm ,its an example how to use it –  Nov 20 '14 at 14:46
  • yes of course :) no problem with that, that's why I didn't downvote. I just think it's not THE BEST solution. (But it's still a working solution, I agree) – alain.janinm Nov 20 '14 at 15:11
  • @alain.janinm I couldn't find any definitive standards specification but it looks like this should be supported everywhere. Take a look at these: https://docs.oracle.com/javaee/5/tutorial/doc/bnajg.html https://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html#5 – Monday Fatigue Sep 07 '21 at 23:36
3

I like using fragments for this, they are standard supported by JSP so no other dependencies are needed. And as you will see it will comes with lot's of other benefits.

Create a tag (parentpage.tag):

<%@tag description="Base page" pageEncoding="UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<%@attribute name="extra_css" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<%@attribute name="header" fragment="true" %>

<html>
<head> ....
// insert css that is needed for every page
<jsp:invoke fragment="extra_css"/>
<jsp:invoke fragment="header"/>
<jsp:doBody/>
<jsp:invoke fragment="footer"/>

Then you create individual pages that inherit from this tag

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:parentpage>

    <jsp:attribute name="extra_css">
        // custom css for this page 
    </jsp:attribute>
    <jsp:attribute name="footer">
        // footer content
    </jsp:attribute>
    <jsp:body>
       // body content
    </jsp:body>

</t:parentpage>
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60
2

Yes! It can be a good option to have all the css and js defination in one jsp and then include that jsp in all pages/jsp.

As you are using spring i would suggest you use tiles https://tiles.apache.org/ that is the much better way to make a standard layout and then use that layout in your all jsp.

Ramzan Zafar
  • 1,562
  • 2
  • 17
  • 18
2

The best solutions is to use JSP Tag File. It allows to encapsulate reusable content using Tag Files : Java doc ref

Here is a good answer with example : https://stackoverflow.com/a/3257426/1140748

Community
  • 1
  • 1
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
2

Make JavaScript and CSS External.

Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests.

Follow these links to get more info on this:

"https://developer.yahoo.com/performance/rules.html"

"include external java script file in jsp page "

Community
  • 1
  • 1
Gaurav
  • 76
  • 4