Import my own class into servlet problem

Hello, everyone.. I fail to import java classes that i create myself into the servlet. I wonder what is the problem. I would feel grateful if anyone can solve my problem. I noe that if it is in the package let say myclass , C:\jakarta-tomcat-4.1.29\webapps\lis\WEB-INF\classes\myclass. It will be
package myclass;
import myclass.*;
But, I put all my class file into below directory not in a package.
C:\jakarta-tomcat-4.1.29\webapps\lis\WEB-INF\classes
and i wish to include let say DBManager.class file into Login.class file which in in the same directory as above. What is the correct code i should put? Thanks for anyone who willing to help me out ~

You don't need to import classes from the same package. They are automatically available in the same namespace. Just use them.
package myclasses;
import myclasses.*;  // this line does nothing
import myctherclasses.*;  // this line is necessary to use classes from another package.Having said that though, it is always preferable to put your classes in a package. Classes in the "unnamed" package are not guarunteed to be available anymore.
Particularly servlet classes which are not in a package aren't found by Tomcat.
In short: always put your classes into packages.
Cheers,
evnafets

Similar Messages

  • Importing a Java class into a servlet...?

    Hi guys..!
    I have a java class that I would like to import or use in my servlet class....does any of you knows how to do that...?? and in which directory should I put the java class..??
    Any help will be appreciated...
    Thanks,

    Same way you would import any other class into any other class. With an "import" statement. There is nothing special about servlets in this case, and there is nothing special about the fact that the class was written by you. The only requirement is that imported classes must be in a package.

  • HELP: Import my own class in JSP ???

    Hi, all!
    I read many previous topics in this forum but no one works for me!
    Please someone help me!
    I'm using Tomcat 4.1.12 and my JSP scripts work just fine, but I need to import my own .class file. I just don't know where to put it so Tomcat can find it!?
    I have this environment variable:
    JAVA_HOME=.;c:\jdk1.3.1
    And in my code I'want to place something like this:
    <%@ page import="myClass" %>
    But I get a "can not resolve symbol" exception.
    Thanks for readind this, and please help if you can!
    Bye.
    adriano

    Within the Tomcat directory, under your application directory place the class file/s into the WEB-INF/classes directory. If your classes are part of a package place the entire directory structure of the package under the WEB-INF/classes directory.
    The import in the JSP is the same as for any Java class.
    Hope this solves your problem.

  • Problem to Import my own classes

    I try to import own classes to unother class
    I have all classes in the same map
    import StockTablePanel;
    when I tried to compile the code it says
    �.� expected
    StockTablePanel;
    What is wrong?

    I try to import own classes to unother class
    I have all classes in the same mapAlso if by the same map you mean the same folder then you don't even have to import the class.

  • Importing my own classes

    I have 2 simple classes in the same directory and I'd like to test the use of import to import one class into the other. Upon compilation of the class thats doing the import, I get a compilation error. Herewith the details, where could I be getting it wrong.
    ****code for class to be imported****
    import java.util.Date;
    public class printer
    public void print()
    System.out.println(new Date());
    System.out.println("Hello World");
    --The above code compiles with no problems.
    ***Code for the main class that imports the above*****
    import printer;
    public class myapp
    public static void main(String[] args)
    (new printer()).print();
    -upon compiling the second class above, myapp.java, I get the following errors:
    C:\java\source>javac myapp.java
    myapp.java:1: '.' expected
    import printer;
    ^
    myapp.java:1: ';' expected
    import printer;
    ^
    2 errors

    Moreover, classes do not need to import any class which is in the same package as their own, and since your printer class is in the default package (if you call that a package) you shouldn't need to import it into myapp anyway

  • Import Adress Book mobiles into Bento - Problem

    Hi I was trying to organize myself by importing some stuff to Bento.The problem I had was that some contacts have several phone numers (work home mobile) and Bento imported work and home. So i tried to create Mobile Phone as a field but the only thing Bento does is to create an empty column, it does not import anything from the mobiles that I have in my adress Book.
    Thank you very much for your help and sorry for my english

    David,
    This was posted in MacFixIt and it works perfectly:
    Entourage addresses into Address Book:
    1. Create a new desktop folder. Name it anything you want, but v-card would be appropriate, because that's what you will be creating.
    2. Open Entourage's Addresses and select the ones that you want to bring over.
    3. Drag these addresses into the v-card folder.
    4. Drag the v-card folder into the Name field of Apple's Address Book. You can safely trash the folder after you are finished.
    Apple addresses to Entourage:
    1. In Apple's Address Book, select the addresses that you want to export to Entourage.
    2. Open Entourage Address Book and drag those addresses into Entourage Address Book. If you are exporting multiple addresses, hold Option while you do this, otherwise only the topmost address will be exported and you'll have to do it one-by-one.
    These methods are not perfect. You may find some minor errors, such as a Home email coming across as Work, but otherwise you'll get everything.
    G5    

  • I am trying to import my own clipart into Word, any easy ways to that. I am using images from Google but have trouble importing into Word

    I am trying to import images from Google into a Word document and would like the easiest way to do that.

    You would probably be best served by posting your question on the Office for Mac Product Forums .

  • Importing existing JFrame class into a new Java Studio Enterprise 8 project

    I have taken over the re-development of some old code, and have imported it into the Java Studio IDE. However, the help files seem to indicate that I cannot use the Form Editor to make changes to the existing JFrame object. It can only be used if you created the object from within the Studio.
    Is this correct? If so, has anyone devised a reasonable work-around besides manually making the changes? (The biggest problem with this approach is that they don't seem to appear in the Form Editor; I would have to make the changes there manually too). The GUI is somewhat complex, and would be a real pain to recreate from scratch.
    Any help is greatly appreciated.
    ~Brian

    The following is a FAQ item on generating form files from existing class files:
    http://www.netbeans.org/kb/faqs/gui-editor.html#FaqFormGeneratingFormFile
    Can I generate the .form file for an existing class?
    Unfortunately no.
    NetBeans form editor is not able to regenerate a missing .form file for GUI classes, or to generate a .form file for GUI classes not originally created in NetBeans.
    However, there are some external tools trying to address this problem, e.g. FormGenerator.
    There are two such utilities available at:
    http://contrib.netbeans.org/servlets/ProjectDocumentList
    ( FormGenerator.zip and FormGenerator2.zip ).
    You may want o backup existing .form files, generate new ones from the classes with either of the above utilities and then compare the two versions.
    Ref: http://swforum.sun.com/jive/thread.jspa?forumID=122&threadID=59475

  • Help! Cant import my own classes?

    Hi all, I really need some help.
    I created a swing browser application in my working folder called Swiki.
    Then I found a open source package that I want to use some of its classes.
    I put that package into a subfolder (cos it has many subfolders itself).
    I am having a problem importing that package into my own browser.java.
    I tried:
    import Kizna.*;
    OR
    import Kizna.src.com.kizna.*;
    when I try to create an instance of one of the classes:
    public HTMLParser myParser
    The error says:
    "HTMLParser.java": Error #: 901 : package com.kizna.html stated in source C:\Swiki\Kizna\src\com\kizna\html\HTMLParser.java does not match directory Kizna.src.com.kizna.html at line 1, column 19
    can someone help?
    thanks
    TC

    When the compiler and runtime are trying to find files, it converts the import statements to directories, so:import com.kizna.html.*;will look under a directory structure "com/kizna/html/". Then, it will look through your classpath and look to see if this directory structure exists in any of the directories specified in the classpath. That means, if your classpath is ".;C:\SomeDir;C:\AnotherDir", it will first look for class files in ".\com\kizna\html\". If it doesn't find the file it is looking for in there, it will look in "C:\SomeDir\com\kizna\html\". If it still doesn't find the file, it looks in "C:\AnotherDir\com\kizna\html\". If the file doesn't exist in any of these, you will get a compiler error.
    Points to remember:
    1)     Classes in a package, when compiled, must always be in a directory tree that matches the package structure. That is, if you have a class called MyClass in package mypackage.subpackage, then the "MyClass.class" file must be in a directory called "subpackage" which in turn is in a directory "mypackage".
    2)     Your import statements must match the actual package name of the class. You must import "mypackage.subpackage.*" above; importing "subpackage.*" will not work.
    3)     Your classpath must consist of all the base directories of your package directory tree. In the above example, your classpath must include the directory that contains the directory "mypackage".
    Thus, the compiler will look through all combinations of <classpaths>/<import package tree>/MyClass.class to find the file for class MyClass. Here, <classpaths> are the paths given in your classpath and <import package tree> are thre directory structures for the packages you import. So, in your case, you want your classpath to include "C:\kizna\classes" and your import statement to be "import com.kizna.html.*". Note that including "C:\kizna\classes\com" in the classpath and importing "kizna.html.*" will not work (even though the combination gives the correct "location") because your import statement does not match the actual package name given to HTMLParser. While this may seem strange that this isn't allowed, it is done to ensure that classes are unique (classes may use the same name if they are in different packages).

  • NullPointerException - importing my own class to JSP

    Hi.
    I'm writing a JSP page. I also made a class in WEB-INF/classes/org/MyXml.class. When I try to run this class from console, there is no problems at all. But if i try to use function from that class in JSP, i get this (the same error i get whichever function i try to use (btw, all functions are static, as they should be, right?)):
    java.lang.NullPointerException
         org.MyXml.CountProducts(MyXml.java:46)
         org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
         org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    I used this class like this:
    <%@ page import="org.MyXml" %>
    <%
    MyXml.CountProducts();
    %>
    I'm also a bit confused, it says that error is on line 46. Well, there's just a comment!
    Please help. Thanks.

    pqeuens,
    MyXml is his class. his methods are static, so, import="org.MyXml.*" won't work. It would either be import="org.*" or import="org.MyXml" (which he has)
    paull1911
    "What file are you looking at to find line 46? It is the java file (a servlet) corresponding to the jsp file."
    It should actually be in the org.MyXml.java file. The stack trace line is:
    java.lang.NullPointerException
    org.MyXml.CountProducts(MyXml.java:46)
    someone_
    You know from the above error line that the error is in MyXml.CountProducts method. After making sure you are working with the most up-to-date version of the class, (if that doesn't help you get a more accurate line number) walk through the CountProducts method and see where you might be trying to access an object that has not yet been instantiated (either using someObject.someMethodOrMember, or someArray[index])

  • Import new java classes into a running programm

    Hello, i have a small Java problem, i hope somebody knows an answer ...
    I try to write a Java Programm, this programm starts and load a new class, this class is not defined inside the running programm, but the running programm should use the new class.
    example:
    // a class file in a directory somewhere at my harddisk
    public void class X
    void doSomething()
    //Print "Hello world"
    start programm A
    Programm A does something, but doesn�t "know" class X, class X is not compiled with program A
    after some seconds, A should load class X.
    if (M == N) "import class X"
    // At this moment, Programm A knows class X
    X NewObjektX = new X();
    NewObjektX.doSometing();
    Thats my problem :)
    I am sorry about my bad english, but its sadly not my first language.

    Hello :)
    Thx for an answer, i tried your solution, but i get another problem.
    The type to create an object ist unknown.
    i?ll post my exmaple code :
    This class is not compiled with the main programm, only the class file is inside the directory of the main program.
    public class SimpleClass
         int Information = 0;
         void printOnScreen()
             System.out.println("Varibale of the class:"
               + this.Information);   
    This is the main programm:
    public class Main {
         public static void main(String[] args)
              try
                Class newClass = Class.forName("SimpleClass");
                Object instance = newClass.newInstance(); 
                  System.out.println(instance.getClass());
                  // At this point no errors, the tpye of instance is
                     "SimpleClass"
                  // This line occurs an error, the type "SimpleClass" is
                  // unknown.
                SimpleClass newClassInstance = (SimpleClass) instance;
              catch (ClassNotFoundException e)
                System.out.println("Can?t open Class");
                System.err.println(e.getMessage());
              catch (InstantiationException e)
               System.out.println("Error1");
               System.err.println(e.getMessage());
              catch (IllegalAccessException e)
                 System.out.println("Error2");
               System.err.println(e.getMessage());
          } // public static void main(String[] args)
    } // public class MainI tried another solution with inheritance, i programmed a superclass, and choose the superclass as the type (the Superclass was compiled with the main programm).
    The SimpleClass extends Superclass.
    // works fine, but i dont want this type of solution :)
    Superclass newClassInstance = (Superclass) instance;
    How cann i solve the "unknown type" problem ?
    SimpleClass newClassInstance = (SimpleClass) instance;

  • How to import  my own class in jsp file

    I have a jsp file in jsp\ directory and a class in web-inf\classes\ directory. How do I import the class in jsp so i can use it in my jsp file. Thanks,

    This is your lucky day, wennie.
    The correct forum to post this is:
    [http://forums.sun.com/forum.jspa?forumID=45]

  • Import my own class

    I created two classes in the same package (myclass). one of them is using another class. what is the syntext of import another class: I tried import myclass.* also import myclass.class1. it didn't work. Thanks,
    j

    For class1.java in mypackage directory:
    package mypackage;
    public class class1 { }
    For class2.java in mypackage directory:
    package mypackage;
    public class class2 { }
    For a file myclass one level out of mypackage:
    import mypackage.*;
    public class myclass {
    public static void main(String[] args) {
    class1 c1 = new class1();
    Something like that should work.

  • How to import GreenSock external classes?

    Hi, I'm having some problems in importing GreenSock external classes into my flash project.
    Line 27: import gs.*;
    Desktop\myFlashAPPS\twitter\src\zainu\twitter\MainClass.as, Line 27
    1172: Definition gs could not be found.
    Here is the directory tree:
    myFlashApp (contains myproject.fla)
         src
              zainu
                   twitter (contains MainClass.as and gs folder)
    MainClass.as:
    package zainu.twitter{

    You could try specifying the path to the gs folder as if you were importing it directly from the myproject.fla file.
    You should set up a classpath for external classes for your system so that Flash has a ready reference for finding them.  YOu can set this up via using Edit -> Preferences... -> Actionscript -> Actionscript 3.0 Settings... -> specify a source path.  Normally what people do is create a folder in a relatively general location and use that as their source path.   Within that they place a "com" folder, and within that com folder they place their subfolders for different classes they acquire from outside sources, such as your gs folder.  So when you want to import the gs classes you can use... import com.gs.*;
    There is a tutorial at gotoandlearn that explains this...   http://www.gotoandlearn.com/play.php?id=30

  • Can't import class into JSP

    Hi,
    I am trying to import a class into my JSP and am getting the following error at runtime:
    org.apache.jasper.JasperException: Unable to compile class for JSP
    An error occurred at line: -1 in the jsp file: null
    Generated servlet error:
    [javac] Compiling 1 source file
    [javac] D:\jwsdp-1.2\work\Catalina\localhost\pe\org\apache\jsp\guestbook_jsp.java:7: '.' expected
    [javac] import MessageGetter;
    My import statement in the JSP is simply:
    <%@ page import="MessageGetter" %>
    and I am placing the class in the WEB-INF/classes directory. This is stumping me - if you've come across this or a similar problem, please let me know. Thanks.
    P

    Try posting your question in the JSP forum (http://forum.java.sun.com/forum.jsp?forum=45)
    cheers,
    Mike

Maybe you are looking for