views:

234

answers:

5

I am writing some jsp and I am wondering if it would be better to have one large include file that I would include with every page or several smaller ones that I would include only on certain pages as needed.

Any given page will only need to call a few methods at most ( < 5). If I use one file, it would be 2500+ lines of code.

My main concern would be about performance with a secondary concern on maintenance.

We are using the jsp include directive <%@ include file="FileToInclude" %>

Edit: I will be doing the same thing for asp pages as well.

+1  A: 

This is Java right? Small include files for sure.

Not to mention it will make refactoring, testing, debugging easier as well.

altCognito
+1  A: 

Having done this is classic ASP - I would say a balance is in order

I would look carefully over the functions / methods / calls you need in all pages and build one large include for those then the smaller less used methods make individual includes - this sort of balances it for performance and ease of building

codemypantsoff
I forgot to mention that I will be doing the same thing for asp. Thanks
Jeremy Cron
+4  A: 

Anything that will be included in every JSP, feel free to add to a single included file. For stuff that will be included on only a small minority of JSPs, create separate included files for those.

Enormous JSPs are a bear to maintain.

Even better, however, is moving as much code as possible out of the JSPs and into your domain objects or into your model or in your servlets. Anything that is pure Java usually belongs not in the JSP but in your JAR files:

  1. IDEs do a better job of navigating and refactoring pure Java code.
  2. When JSPs are mostly HTML and JSP tags (with as little Java code as possible), they become easier to maintain and understand.
  3. It's easier to understand pure Java as there are no HTML tags mixed in.
  4. It makes the JSPs easier to understand as a huge block of Java code is replaced with a very short call to Java code.
  5. It makes maintenance of your JSPs easier without making maintenance of your code base more difficult.
Eddie
+2  A: 

You should use JSPs only for view purposes. Business logic should not be placed JSP files, but in plain java classes. Please read up on Model-View-Controller design patterns. See this wikipedia article

Kees de Kooter
A: 

In ASP I have one include file that is used on every page...but that one file has about 5 or more includes in that. This way I can break the functions into logical groups (which makes it easier to find things, and safer when updating one file only) but only need remember one line when creating new pages that need the functions.

Magnus Smith