Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

class files, packages and servlets in tomcat

  • 05-07-2007 05: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, Registered Users 2 Posts: 12,025 ✭✭✭✭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, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭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