ChatGPT解决这个技术问题 Extra ChatGPT

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?

Not to mention that this is not the best practice ;)

E
Eddie

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

Should you put it all on one line or split it across multiple lines for readability?
Like me, do not forget the "@", you cannot write that directly into a sciplet <% %>
Maintaining 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.
Use a taglib if you can! and the c tag lib inside it... nested tutorialspoint.com/jsp/jsp_standard_tag_library.htm
I never expected that we could actually import more than one class in a single import statement ever in Java. This is amazing. XD
n
naXa stands with Ukraine

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


Down vote for providing a lecture instead of an answer to the question.
upvote for providing a lecture instead of an answer to the question
+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.
Call 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.
@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?
j
jjnguy

In the page tag:

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

Why 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?
Axeman, java,lang is OK but java.util classes doesn't get included automatically. or IS it the case with JSP's ????
G
Georgios Syngouroglou

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 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).
A
Alex Ciocan

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 .


A
Avinash

This is the syntax to import class

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