Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Packages and Servlets

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Packages and Servlets

    Respected PROGRAMMERS,
    In Apache tomcat, there was a project created that included Servlets to service the incoming requests. And java class files were kept under




    WEBINF\classes\com\example\web




    Please tell me why the java files
    need to include "package com.example.web" statement so
    that their compiled classes may be kept in com\example\web folder.
    Tomcat container can easily locate the class file because of the servlet mappings.
    Why this package statement necessary?


    Please forgive if you get angry at this.
    Thank You SIR!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Packages and Servlets

    include "package com.example.web
    The package statement is so that the java program will have the correct classname when accessing a class. The full class name will include the package name:
    com.example.web.YOURCLASSNAME
    The classpath points to the folder containing the com folder. The package path gives the jvm the location of the YOURCLASSNAME.class file.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Packages and Servlets

    Thank You Sir, Servlet class files are kept under web folder. And Servlet mappings are used to facilitate the container to find these classes files.When Container has found them, why can't jvm find the classes automatically?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Packages and Servlets

    why can't jvm find the classes automatically?
    Where should the JVM look for classes? You must give the jvm a classpath telling it where to look for class definitions.
    Some programs can create their own classloaders to have the jvm be able to load classes from other folders. Those other programs could do a search for classes if they wanted.
    Ask the author of the programs you are using why they don't have their program search for classes.

Similar Threads

  1. Meaning of Java Packages
    By Effrego in forum Java Theory & Questions
    Replies: 1
    Last Post: February 5th, 2011, 04:18 PM
  2. Problem with Packages
    By pirezas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 12th, 2010, 02:41 PM
  3. [SOLVED] Confuced when import packages...
    By Delmi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 20th, 2010, 03:39 AM
  4. [SOLVED] placing packages of the same directory into an array
    By javanub:( in forum Java Theory & Questions
    Replies: 16
    Last Post: May 18th, 2010, 07:49 AM
  5. importing packages..
    By chronoz13 in forum Java IDEs
    Replies: 4
    Last Post: November 23rd, 2009, 04:49 PM

Tags for this Thread