Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

class files, packages and servlets in tomcat

Options
  • 05-07-2007 5:12pm
    #1
    Closed Accounts Posts: 658 ✭✭✭


    Hi

    A simple question but one that has been racking my brains for a while, so I thought I should ask here. I am using tomcat 5.1.1.7.

    I have a servlet which I use to parse form data from a jsp page in my web application. At the top of this servlet I have the following line of code, which I understand to be correct

    package com.organisation.webappname;
    // then all the needed imports, class declaration and methods etc, all work fine

    This servlet also has itself defined and mapped in my the WEB-INF/web.xml file in the application directory.

    <servlet>
    <servlet-name>FormProcessingServlet</servlet-name>
    <servlet-class>com.organisation.webappname.FormProcessingServlet</servlet-class>
    </servlet>

    I have a few classes that I need also, like a class written for simple database connectivity and queries. This class also has the following lines at the top

    package com.organisation.webappname;
    //then all the needed imports etc etc same as above.

    What I want is for the Servlet class that I'm using to make use of this class file, and I'm having trouble doing that.

    Have tried adding the class files needed to a jar file and putting that into the WEB-INF/lib directory, but that only works if I remove the package name declaration from the top of these class files, compile these class files, jar them and put the resulting jar file into the WEB-INF/lib directory. I have this jar file defined under the CLASSPATH windows environment variable. If I remove the package declaration from the servlet they will compile, but I can't do this as I cannot map the servlet in the WEB-INF/web.xml file.

    I know this has been done before and I've been searching the web but if someone had a simple, straight one sentence answer I would be very grateful. any more info you need please let me know.

    Thanks


Comments

  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    What's your folder structure look like.


  • Closed Accounts Posts: 658 ✭✭✭pontovic


    theres the document root in webapps, lets call it sample_app, and then within that there is the following:

    WEB-INF/web.xml
    WEB-INF/classes/com/sample_app/package/<class-files>
    WEB-INF/lib/<jar-files>

    and then in the document root there are all the image files, jsp files and css files etc associated with the website.


  • Closed Accounts Posts: 658 ✭✭✭pontovic


    If I put the class files into a jar file and put this jar file into WEB-INF/lib, and then put into my servlet the import statement 'import com.example.package.*;' that works, so the classpaths seem correct. The problem now is that the class files within this jar file aren't being picked up.

    I have the package name declarations at the top of each class file that says 'package com.example.package;', but still it won't work. I know there is a simple answer for this but I just can't find it and believe me I've been trying. If someone can help please do and I'd be forever grafeful.

    Thanks


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    If you are putting those files exploded in the filesystem, then they must match the package name

    i.e.

    WEB-INF/classes/com/organisation/webappname/MyDBConnectivity.class

    or if you want to package these in a jar and put them in WEB-INF/lib,

    then the contents of the jar file must also be

    /com/organisation/webappname/MyDBConnectivity.class
    etc....


  • Closed Accounts Posts: 658 ✭✭✭pontovic


    Excellent, thank you Lynchie. I solved this problem earlier and then just saw this post. Websites explaining these things should make this point alot clearer. Thanks for your help guys.


  • Advertisement
Advertisement