Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice: custom jsp tag or include tag?

Tags:

java

jsp

What is the best practice for reusable jsp components in your code? Writing a custom tag or using the <jsp:include> tag?

Which circumstances would dictate the use of one or the other?

Use case: I'm trying to refactor part of an application and would like to make an informed decision on how to break down the html into reusable pieces. I'm currently using a custom tag to build a layout including page header and footer and the html head and dependencies. But I'm unsure of the best way to include a chunk of static html that's reused across the app.

like image 615
Josie Avatar asked Jul 30 '15 15:07

Josie


People also ask

What are the advantages of JSP custom tags?

Advantages of Custom Tags in JSPExpressive – It provides a wide range of functionalities, scripting elements to the page authors. Usable from different scripting languages – This functionality can extend to other scripting languages. No need for scriptlet tag – Once we create a custom tag, we don't need scriptlet tag.

What is difference between custom JSP tags and beans?

Custom tags usually defined in relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page. 5. Custom tags are available only in JSP 1.1 and later versions, but beans are available in all JSP 1.

Can we create custom tags in JSP?

You can include one or more custom JSP tags in a tag library. You define a tag library by a tag library descriptor ( . tld ) file. The TLD describes the syntax for each tag and ties it to the Java classes that execute its functionality.


1 Answers

There are really a number of reasons to go with one or the other from a codebase organization standpoint.

A primary reason to create a Tag Library instead of using the jsp:include tag is to provide parameters and values that might influence the markup generated. Say you want to pass a username to appear in your page header that you've spent precious cycles retrieving already. Also if you have especially complicated logic (i.e. anything more than conditionals or loops), a Tag Library is the way to go. Nobody wants a mess of spaghetti code sitting in a JSP.

I would say that a tag library that accepts no parameters that always produces static output is functionally equivalent to using jsp:include.

Your specific use case involves somewhat related markup. You have a page header and a page footer. It might make sense to implement this as different tags within the same Tag Library class, to keep these related snippets in the same location.

On the other hand, encapsulating HTML inside a Tag Library reduces the maintainability for non-developers. Say you have a graphic designer on a project that doesn't know a lick of Java but wanted to edit the markup. Sure, they might be able to wade through a tag library class and figure out what they need to change; maybe not!

To sum up, if you think you might pass parameters, or you have business logic, it's Tag Library all the way. If it's just static content and/or you want non-developers to help edit, maybe stick with an included .jsp.

like image 179
doublecompile Avatar answered Oct 22 '22 06:10

doublecompile