Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Custom Classes

Status
Not open for further replies.

petey

Programmer
Mar 25, 2001
383
US
Just getting started with JSP, so I need some help.

When you create your own class definitions, how do you use those classes in your JSP's? Also, where do you put the class files?

(I tried putting the class filed in the same directory as the JSP, but it didn't work)

Thanks
 
You need to (ok not really) define the package the class lives in. This is done with the first non-comment line in your Java file.
Code:
package com.mytestpackage;
To get the class to work it must be in an appropriate directory structure. Packages work just like DNS resolution. Java will look the classpath for a com package. Then it will look for the mytestpackage with com. Then it will look for your class with mytestpackage package. So the file structure would be:
Code:
classes
   - com
      - mytestpackage
The directory classes would be added to the classpath. The source files directory should be setup in a similar fashion, I recommend:
Code:
src
   - com
      - mytestpackage
Then all you have to do is import the class in your jsp the page directive and you can use any classes defined within that package.
Code:
<%@ page import=&quot;com.mytestpackage.*&quot; %>
Alternately you could always refer to the class by the fully-qualified class name and skip the imports.
Code:
com.mytestpackage.MyClass temp = null;
I know this is probably alot of information to take in but I hopes this helps. BTW this is really more of a Java question not a JSP. Wushutwist
 
You need to (ok not really) define the package the class lives in. This is done with the first non-comment line in your Java file.
Code:
package com.mytestpackage;
To get the class to work it must be in an appropriate directory structure. Packages work just like DNS resolution. Java will look the classpath for a com package. Then it will look for the mytestpackage with com. Then it will look for your class with mytestpackage package. So the file structure would be:
Code:
classes
   - com
      - mytestpackage
The directory classes would be added to the classpath. The source files directory should be setup in a similar fashion, I recommend:
Code:
src
   - com
      - mytestpackage
Then all you have to do is import the class in your jsp the page directive and you can use any classes defined within that package.
Code:
<%@ page import=&quot;com.mytestpackage.*&quot; %>
Alternately you could always refer to the class by the fully-qualified class name and skip the imports.
Code:
com.mytestpackage.MyClass temp = null;
I know this is probably alot of information to take in but I hopes this helps. BTW this is really more of a Java question not a JSP question. You would probably get a quicker response by posting on the Java board. Wushutwist
 
i import user class in jsp file.

I put this class in my jsp files directory.
if i want to import that class in my jsp file what to do,
again notice that class file and jsp file both are in same
directory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top