Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In JSP, How to import a java class that exists in default package?

 
Lakshman Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have questions about import java class in JSP,like the following structure

test
|
|__WEB-INF-->classes-->Dog.class
|
|__index.jsp

In classes directory,I put a java class named Dog.class,and I put a JSP file
in test directory,named index.jsp ,and In index.jsp called Dog class,but I
don't use "import",I think JSP can find it.

/*Dog.java*/
public class Dog{
String name;
public Dog(String a){
this.name=name;
}
public String getName(
return name;
}
}

But when I run index.jsp,it raised error:
cannot resolve symbol : class Dog
Then I copy Dog class into test directory,it still raise above errors.

My questions are:
1.I want to know what default path which JSP use class
2.I don't want to use "package" in Dog.java, If I use import in JSP,how to
write this import statement?

Thanks in Advance.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to put that class into a package. With earlier JVMs you could use classes in the default package, but not any more (I think since Java 5).
 
Lakshman Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya, i even created a folder (foo) under classes directory and placed the Dog.class file and tried it out...but the result is same as earlier (an error).

test-->web-inf-->classes-->foo-->Dog.class

In jsp, i placed <%@ page import="foo.*" %>.
In Dog.java, i placed package foo; as the first line and compiled successfully.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic