Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import java file - error in JSP

 
Amit Delve
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am using Tomcat for my JSPs.
My jsp pages are present in the tomcat/webapps directory under my application.
The structure of my application is as follows
tomcat
|
webapps
|
myapp
|
jsp - (contains my jsp pages)
|
page1.jsp
page2.jsp
|
classes - (contains my class files)
|
com
|
project
|
javafiles
|
class1.class
class2.class
|
lib - (contains my jar files)
I am trying to import class1.class in my jsp pages eg. page1.jsp.
I use the standard import file syntax in my jsp page -
@import com.project.javafiles.* .
When I do that I get an error stating that file cannot be imported.
Is there anyway i can import my java objects in my JSP and use them as plain java objects without using the usebean directive.
What I mean is can i use normal java code for object instantiation in the jsp page.
Please reply asap.
Thank you very much.
Delve.
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure your directory structure is right. The class files should be under yourApp/WEB-INF/classes and you recreate the package structure as database under that.
[ January 02, 2004: Message edited by: Tim Baker ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Delve,
First, in order for your classes to be in Tomcat�s classpath, you need to use a proper Web Application Directory Structure. They must be in the directory <webapp-root >/WEB-INF/classes (so in your case <webapp-root>/WEB-INF/classes/com.project/javafiles/) and your jars in <webapp-root >/WEB-INF/lib
Take a look at Tomcat's Application Developer's Guide for more info. You can also Google "Web Application Directory Structure".
Once your files are in the correct directores, you can import them in a JSP using the following page directive:

You can then use the classes in scriptlets:

You should note, however, that since JSP v1.2 scriptlet use is discouraged in place of tag libraries, including the JSTL (JSP Standard Tag Library). Scriptlets muddy up the view presentation with business logic. Under a good MVC (Model View Controller) design, the presentation markup (i.e. html) and business logic (java code) should be kept separate.
HTH
 
Amit Delve
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Thank you for your reply.
The thing is that the directory structure I mentioned in my previous mail may not be right because I am not infront of my development machine right now.
I am sure my directory structure is right in my development machine because I could use my java class file through the <jsp:usebean> and it worked. But the import would not work.
I even created a .war using the same structure with the jsp and java files having <jsp:usebean> and it worked too.
So I am pretty sure that my directory structure is right because "usebean" is able to load my class properly. It is just that when I am trying to import the class and use it, it fails.
Please do provide feedback.
Thank you,
Delve.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Delve!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
JSP Forum Bartender
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delve,
Can you post a copy of the exact error (with a stack trace if applicable) you�re getting. They would probably help us track down your issue.
 
Amit Delve
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Sure I will do that. I am at a friends place right now. Will do that in a couple of hours. Thank you very much.
Delve.
 
Amit Delve
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Well I think the import problem got resolved somehow. I am not receiving any errors right now. I dont know how - probably a reboot of the machine solved it , not too sure.
thank you,
Amit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic