Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you import classes in JSP?

Tags:

java

jsp

People also ask

How do you import a class?

To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.

What is class file in JSP?

The class files are compiled into the web module's WEB-INF/classes directory. The class files are compiled into the WebSphere Application Server temp directory, usually profile_root /temp. compileToDir is supplied; compileToWebInf not supplied, or is either true or false.

How do I import one JSP file to another?

To include JSP in another JSP file, we will use <jsp:include /> tag. It has a attribute page which contains name of the JSP file.


Use the following import statement to import java.util.List:

<%@ page import="java.util.List" %>

BTW, to import more than one class, use the following format:

<%@ page import="package1.myClass1,package2.myClass2,....,packageN.myClassN" %>

FYI - if you are importing a List into a JSP, chances are pretty good that you are violating MVC principles. Take a few hours now to read up on the MVC approach to web app development (including use of taglibs) - do some more googling on the subject, it's fascinating and will definitely help you write better apps.

If you are doing anything more complicated than a single JSP displaying some database results, please consider using a framework like Spring, Grails, etc... It will absolutely take you a bit more effort to get going, but it will save you so much time and effort down the road that I really recommend it. Besides, it's cool stuff :-)


In the page tag:

<%@ page import="java.util.List" %>

In case you use JSTL and you wish to import a class in a tag page instead of a jsp page, the syntax is a little bit different. Replace the word 'page' with the word 'tag'.

Instead of Sandman's correct answer

<%@page import="path.to.your.class"%>

use

<%@tag import="path.to.your.class"%>