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
?
6 Answers
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" %>
-
9Should you put it all on one line or split it across multiple lines for readability? – Xonatron Jan 24 '12 at 20:07
-
6Like me, do not forget the "@", you cannot write that directly into a sciplet <% %> – Heetola Jan 31 '13 at 21:10
-
7Maintaining a list of imports, spotting duplicates, sorting etc. will be ***much*** easier if you **do** **not** put all of them on one line. In fact I'd go as far as saying that I'd highly recommend ***against*** putting them all on one line. – scunliffe Aug 28 '14 at 14:48
-
Use a taglib if you can! and the c tag lib inside it... nested http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm – tgkprog Feb 16 '15 at 07:25
-
I never expected that we could actually import more than one class in a single import statement ever in Java. This is amazing. XD – progyammer Oct 29 '18 at 19:02
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 :-)

- 35,493
- 19
- 190
- 259

- 16,067
- 8
- 44
- 68
-
-
57Down vote for providing a lecture instead of an answer to the question. – Willis Blackburn Feb 11 '12 at 15:10
-
81
-
8+1. Sometimes shining light in the right direction is necessary when the OP seems to be in darkness and doing things in not the right way at all. – adarshr Apr 30 '13 at 17:24
-
3Call me an heretic but I very often use Lists (and plenty of other POJO) in JSPs. This is of course strictly limited to proof of concepts and small do-it-all JSPs that are meant for one very specific and temporary purposes. I've yet to find a faster way to build a small webclip / webpage highly specialized. Of course, when writing a "real" app / website, this is not even considered. – pieroxy Jun 22 '13 at 16:03
-
5@KevinDay Pardon my ignorance, but in short how is accessing a List breaking MVC? If I'm modifying the list, sure, but if I'm just reading items from it, does that violate MVC principles? – Mar Dec 02 '14 at 18:52
-
1@MartinCarney If the page is interacting directly with the list, then it is accessing the data model directly, which creates a static dependency between the view and the model - if the model changes, all of the views that do this are going to break. A pure MVC approach would use a custom tag that takes care of rendering the list content. If the model ever changes, then only the custom tag gets changed. Refactoring like this is much, much easier with tags. Note that not every situation calls for a 'pure' approach, but it's important to at least be aware of the tradeoffs. – Kevin Day Dec 03 '14 at 13:19
-
@KevinDay Actually I think your 'mvc' is a bit outdated/shortsighted. Yes, its nice to have view/model separation, however, sometimes you do need to pass data through your jsp (like to a js file), and creating a whole bunch of infrastructure to stay 'mvc' is not the sane answer. I'm all for doing things the correct way, but sometimes its good to remember that the approaches that were good in your day may not be so good anymore. – smaudet Aug 18 '16 at 14:20
-
@smaudet - the reason the answer seems outdated is because I wrote the answer in 2008. I'm still surprised at the amount of controversy here. In my defense, I will point out that the original poster actually *thanked* me for the guidance I gave them. So the person who asked the question got the info they needed, despite folks thinking I was giving a lecture. Oh well! – Kevin Day Aug 31 '16 at 21:48
-
Writing Java code in JSPs is bad but JSP allows that. There are other Java UI frameworks, like Wicket, where it is simply not allowed but that's because it's completely unnecessary as Wicket has a very awesome model which has excellent separation of concerns between the markup (HTML) and the code (Java). – Volksman Oct 18 '16 at 20:51
-
@Volksman doing stupid things in JSPs is pretty much always bad, not all code in jsps is bad, or rather, there are different (probably better) ways of doing things than using jsps. Sometimes one needs to put code into a jsp to avoid doing a stupid thing (like maybe using said jsps). – smaudet Dec 14 '16 at 10:43
-
@smaudet Some would say using JSP in 2016 is a stupid thing :) with the quality of Component Oriented Java UI frameworks available these days: Wicket, JSF, Tapestry are some Java Component Oriented UI frameworks. Angular 2 is really showing that Component Oriented UIs are really a 'thing' now but it means writing your UI in a completely different language (JS, Coffee or TypeScript) to your server side code. Most good Java component oriented frameworks can deliver the same yummy goodness that Angular 2 delivers but in a single language, easy to develop, understand, debug and maintain solution. – Volksman Dec 15 '16 at 19:46
-
@Volksman sure, and sometimes you are stuck with stupid decisions others have made. Using a little bit of code to fix this isn't harmful. If you're in the position to, it puts you on the path to using better approaches. I wouldn't write a component ui framework inside of a JSP, granted, but I think it is perhaps this stupid opinion that you should 'never' use code in a jsp that led to it taking so long for e.g. the component orientated approach to latch on. – smaudet Dec 15 '16 at 20:21
-
1Sorry but I have to add that this "using a list is breaking MVC" is just nonsense. Whether you use tag libs or not you are still just doing the exact same thing, it's just that the tag lib hides the "List" syntax from you. There is always a "static dependency" between the view and the model. You can hardly write a view for a model without a dependency because the view is always specifically for that model! If you render the model in multiple places and want to have minimal code changes if the model changes then you just use a partial view (which is of course what a custom tag would be!) – chris Feb 22 '17 at 23:39
In the page tag:
<%@ page import="java.util.List" %>
-
1Why would you need to import File and IOException classes OR is it that you were just informing the questioner about how to import more than one class in the JSP? – Vijay Dev Oct 27 '08 at 08:10
-
2Axeman, java,lang is OK but java.util classes doesn't get included automatically. or IS it the case with JSP's ???? – akjain Oct 30 '09 at 09:21
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"%>

- 18,813
- 9
- 90
- 92
-
This helped me, my IDE was telling me the `<@page` annotation didn't exist and the resulting stack traces weren't much help. But all went away when I provided the required imports. (mine was to do with migrating from `java.util.Date` to the `java.time` API). – Bill Naylor May 10 '22 at 18:43
-
what about non java.util files? What should the folder structure look like? – john k Oct 11 '22 at 21:04
This is the syntax to import class
<%@ page import="package.class" %>

- 4,115
- 2
- 22
- 41

- 663
- 12
- 24
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 .

- 2,272
- 14
- 20

- 502
- 5
- 15