Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Packages

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

Shashank
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic