Basic Introduction to Import Classes in JSP
Reusable components or libraries are one of the most important features of good software. Libraries, once created, reduce development time for future projects. One important technique for using existing classes in JSP pages is by using the import directive. There are many existing Java applications that have a lot of core logic implemented in Java classes. For example, consider an employee payroll processing application. The logic for calculating employee bonus pay out and stock options has been written in Java. The application is revamped to use JSP, AJAX and Hibernate but you still want to continue using this core bonus calculation class instead of rewriting it. JSP provides a method to import Java classes using “directives.” Here, we will look at importing Java classes in JSP pages.
Directives are JSP’s mechanism for providing specific instructions to the container when it translates JSP to servlet code. The directives supported by JSP are categorized as –
- Import directive
- Page directive
- Taglib directive
We will be discussing the import directive in detail in this article. As the word suggests, this directive is used to import the contents of another file into this JSP page. The import directive can be used for different resources such as Java classes, other JSPs or HTML files. This is one of the most frequently used directives in JSP code.
Working of import directive
The import directive is used at compilation time and its concept is similar to the Java import class directive. The collection of multiple classes together organized in a hierarchy is referred to as a package. For example, you may have a package called “empsalary” that may be structured as – empSalary
Now a developer who is working on calculating the annual bonus pay out in his method can include the corresponding package using the Java statement –
Another module that is handling esops pay out can include the corresponding package using the Java statement –
The concept of importing a package means that the compiler is aware of where it can look for symbols and doesn’t make the resulting jar file any bigger in size. You can also import any 3rd party packages such as Xerces parser for XML files. The import directive thus facilitates the integration of external libraries in your code very easily with minimal overheads.
Importing in JSP
The concept of import can be used in JSP too, but the syntax is slightly different. Consider the following example:
<%@ page import="empSalary.esops.*" %> <HTML> <BODY> <% System.out.println( "Calculating esops pay out for employee" ); Esopesop = new Esop(“123A”); %> ESOPs allocated are<%= esop %> </BODY> </HTML>
The first line here is the import directive that begins with the sequence “<%@”. The example shown here is called as a page directive. The page directive can be used to import multiple packages using a comma separated list:
<%@ page import="java.util.*, empSalary.esops.*" %>
Alternatively you can also use multiple import directives. For example:
<%@ page import="java.util.*" %> <%@ page import="empSalary.esops.*" %>
You can also import a specific class as follows :
<%@ page import="empSalary.esops.proratacalc" %>
The import page directive is applicable for the entire JSP page and the position is not relevant. So even if the directive is placed at the end of the JSP, the package contents would be visible to all declarations, expressions and scripts used on the page. The concept to import in Java differs slightly because the classes are loaded on the web server.
There are certain default packages that are imported in JSP. The list of packages is as follows:
There may be slight variations in the default imports for each version.
When you are importing classes using the page import directive in JSP, it’s important to place the class files in the correct location. The classes that are already packaged in the form of a jar file can be directly placed in the web application’s WEB-INF/lib directory. If the class files are present in the form of a package, they should be placed in the WEB-INF/classes directory. For example, your package is empSalary.esops then the directory structure will be WEB-INF/classes/empSalary/esops. The class files from this directory will be picked up correctly by the web server as this class path is always searched. If the classes are not placed in the right structure at this point, then the class loading will fail and you will have an error while loading the JSP.
For Java developers who have just moved to servlet technology, it is often very tempting to use more Java code and code lesser in the JSP template. Too much of Java code on the JSP page is not a good design practice and leads to maintenance and security issues. The developer has to draw the line between the business logic code and the view code. Anything that belongs to the view must be inside the JSP and anything that is part of the business logic must be in Java. If the Java piece is a reusable class, then it can be packages and imported in multiple JSPs. Debugging errors in the Java class library can be frustrating at times since the error messages that originates from the server tend to be cryptic and vague.
As you might have noticed, it is very simple to import classes in a JSP page. The concept is syntactically easy to understand and the key for developers is to identify the right packages to import.The onus on the developer is to identify the parts of the implementation logic that can be created as reusable class packages and imported in multiple JSPs as required. The developer has to consider this in the design phase and use import as per the design.
Last Updated September 2022
What is JSP? - JSP Course + Certificate of Completion - Develop Java Web Apps with JSP & JSTL - Become a JSP Developer | By Morteza KordiExplore Course
JSP students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.