[java] How do you import classes in JSP?

I am a complete JSP beginner. I am trying to use a java.util.List in a JSP page. What do I need to do to use classes other than ones in java.lang?

This question is related to java jsp

The answer is


In the page tag:

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

In the page tag:

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

Use Page Directive to import a Class in JSP page. Page Directive Uses 11 Different types of Attributes , One of them is "import". Page Directive with import Attribute Allows you to Mention more than one package at the same place separated by Commas(,). Alternatively you can have multiple instances of page element each one with Different package .

For Example:

 <%@ page import = "java.io.*" %>
 <%@ page import = "java.io.*", "java.util.*"%>

Note : the import attribute should be placed before the element that calls the importd class .


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 :-)


Use Page Directive to import a Class in JSP page. Page Directive Uses 11 Different types of Attributes , One of them is "import". Page Directive with import Attribute Allows you to Mention more than one package at the same place separated by Commas(,). Alternatively you can have multiple instances of page element each one with Different package .

For Example:

 <%@ page import = "java.io.*" %>
 <%@ page import = "java.io.*", "java.util.*"%>

Note : the import attribute should be placed before the element that calls the importd class .


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"%>

This is the syntax to import class

  <%@ page import="package.class" %>

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 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"%>

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 :-)