File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Default Packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Default Packages" Watch "Default Packages" New topic
Author

Default Packages

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Guys,

I was just wondering, that why do we need to import the default packages such as <%@ page import="java.util.*" %> to use ArrayList in our JSP.
<% ArrayList f = new ArrayList();
f.add("A"); out.println(f); %>

Technically the above line of code, goes inside the service method & thats a regular java code. Any to any java code, java.util* is by default imported. Am i correct? If yes, then why do we need to import java.util in jsp to use collection related stuff...

thanks
Shashank


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi i think you will find that java.util is not imported by default. Evan in a regular java class you have to import the package to use any of the Collections.


Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi Shashank -

java.util is NOT imported by default - you're probably thinking of java.lang

regards,
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Hi,,,

By default Java.util.& is imported...

Shashank
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi Shashank -

I just checked the generated java source code for one of my JSPs that doesn't use the page directive to import anything. The only imports I have are:

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

which you would expect the engine to put in. Now, I'm using Sun Application Server 8.2 - maybe you're using something different which operates differently, but I'm 99% sure that it's not a requirement for a JSP to automatically import java.util.*

If your container is doing that import, well I've no idea - I just wouldn't count on it being a portable feature to other containers!

Regards,
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
I did the same for tomcat and as would expect just the same.

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Thanks, I guess I was getting confused with Java.lang.*. Thanks

Shashank
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Default Packages
 
Similar Threads
Import package
recursive function usage in JSP
Collections and Generics Help
Generics Casting
calling same jsp on submit button