Wednesday, 15 January 2014

Using @page import in JSP

jsp import example
Here is a simple example that illustrates using @page import in JSP.

In this example, you'll learn how to import java packages in JSP using the @page directive so that we can use classes directly in JSP program without the need of mentioning the full classpath while using other classes.
Folder structure:
webapps
            |_ jsp1
                     |_ index.jsp

index.jsp


<%@page import="java.util.*"%>
<%
    ArrayList<String> as=new ArrayList<String>();
    as.add("One");
    as.add("Two");
    as.add("Three");
    as.add("Four");

        for(String k:as)
        out.print(k+"  ");
%>

The first tag imports the java.util package so are we able to use the ArrayList without the need of writing java.util.ArrayList.
You can also add as many number of page imports as you want either in single tag or multiple tags. For example,

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

No comments:

Post a Comment