• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Default Packages

 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shashank -

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

regards,
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,,,

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

Shashank
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I guess I was getting confused with Java.lang.*. Thanks

Shashank
    Bookmark Topic Watch Topic
  • New Topic