Java/TomCat How to Configure a Servlet contextPath

Discussion in 'Java' started by Rich Morey, Oct 31, 2008.

  1. Rich Morey

    Rich Morey Guest

    Hi --

    I have Tomcat running locally on my PC. I have built a simple Java
    application that runs as expected when I typ in the URL:

    http://localhost/javaApp/TestClass

    The app runs this way because I am planning to have multiple classes
    within "javaApp." My question is, how would I configure my server so
    that this URL:

    http://localhost/javaApp/TestClass/Action1

    would load TestClass? Currently I get a 404 with the above URL. I
    believe, from what I have read, that this relates to configuring the
    context.xml fill however, I have so far been unsuccessful in
    understandin he docmentation well enough to make this work.

    Any help would be appreciated!

    Thanks

    Rich
    Rich Morey, Oct 31, 2008
    #1
    1. Advertising

  2. Rich Morey

    Mark Space Guest

    Rich Morey wrote:

    >
    > http://localhost/javaApp/TestClass/Action1
    >
    > would load TestClass? Currently I get a 404 with the above URL. I


    This is correct. However, if your web.xml file isn't set correctly,
    this won't load. Want did you do to configure the web.xml? Can you show us?
    Mark Space, Oct 31, 2008
    #2
    1. Advertising

  3. Rich Morey

    Lew Guest

    Rich Morey wrote:
    >> http://localhost/javaApp/TestClass/Action1
    >> would load TestClass? Currently I get a 404 with the above URL. I


    Read
    <http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html>

    It is highly unusual to refer to classes directly in the app URL.
    Instead one refers to mappings to servlets (ideally JSPs in most
    cases), which as Mark Space said are set in the web.xml.

    In your example URL (aside: the words "Java" and "Class" in the URL
    are bad practice), "javaApp" is the application context and
    "TestClass" is the servlet or page identifier.

    I'm not sure what you mean by the word "load" in "would load
    TestClass?". "Loading a class" has a meaning different from
    "displaying a page". What the URL does is identify to the app server
    (e.g., Tomcat) what page you want to display, or alternatively, what
    servlet you want to invoke. Loading a class is what Tomcat does, or
    may do, as part of accessing the specified resource, but is by no
    means what the URL signifies.

    --
    Lew
    Lew, Oct 31, 2008
    #3
  4. Rich Morey

    Rich Morey Guest

    Hi --

    Thank you both for your quick replies.

    > In your example URL (aside: the words "Java" and "Class" in the URL
    > are bad practice), "javaApp" is the application context and


    I am aware of this. I just did that for my own learning curve.

    > This is correct. However, if your web.xml file isn't set correctly,
    > this won't load. Want did you do to configure the web.xml? Can you show us?


    I should say that I am just learning Java & Tomcat at this point. My
    web development experience prior to this was with Delphi. When
    building a Delphi web app a URL would look something like this:

    http://host/library.dll/action?

    In this case, the "library.dll" file could/would contain multiple
    "actions" that could be invoked like this:

    http://host/library.dll/action1?
    http://host/library.dll/action2?

    etc. This would allow me to develop related functionality within a
    single library. For example, I recently completed a payment processing
    DLL for a client. In this case the URLs were something like this:

    http://host/payment.dll/Cash?
    http://host/payment.dll/Check?
    http://host/payment.dll/CreditCard?

    Each action, in this case "Cash", "Check", "CreditCard" was invoked
    from within the single library, "payment.dll". I would like to
    replicate this kind of configuration within Java, mostly because this
    has been the methodology I have used in the past and so I "think"
    along these lines.

    Thanks

    Rich


    > "TestClass" is the servlet or page identifier.
    >
    > I'm not sure what you mean by the word "load" in "would load
    > TestClass?".  "Loading a class" has a meaning different from
    > "displaying a page".  What the URL does is identify to the app server
    > (e.g., Tomcat) what page you want to display, or alternatively, what
    > servlet you want to invoke.  Loading a class is what Tomcat does, or
    > may do, as part of accessing the specified resource, but is by no
    > means what the URL signifies.
    >
    > --
    > Lew
    Rich Morey, Oct 31, 2008
    #4
  5. On Oct 31, 2:23 pm, Rich Morey <> wrote:
    > Hi --
    >
    > Thank you both for your quick replies.
    >
    > > In your example URL (aside: the words "Java" and "Class" in the URL
    > > are bad practice), "javaApp" is the application context and

    >
    > I am aware of this. I just did that for my own learning curve.
    >
    > > This is correct.  However, if your web.xml file isn't set correctly,
    > > this won't load.  Want did you do to configure the web.xml? Can you show us?

    >
    > I should say that I am just learning Java & Tomcat at this point. My
    > web development experience prior to this was with Delphi. When
    > building a Delphi web app a URL would look something like this:
    >
    > http://host/library.dll/action?
    >
    > In this case, the "library.dll" file could/would contain multiple
    > "actions" that could be invoked like this:
    >
    > http://host/library.dll/action1?http://host/library.dll/action2?
    >
    > etc. This would allow me to develop related functionality within a
    > single library. For example, I recently completed a payment processing
    > DLL for a client. In this case the URLs were something like this:
    >
    > http://host/payment.dll/Cash?http://host/payment.dll/Check?http://host/payment.dll/CreditCard?
    >
    > Each action, in this case "Cash", "Check", "CreditCard" was invoked
    > from within the single library, "payment.dll". I would like to
    > replicate this kind of configuration within Java, mostly because this
    > has been the methodology I have used in the past and so I "think"
    > along these lines.
    >
    > Thanks
    >
    > Rich
    >
    > > "TestClass" is the servlet or page identifier.

    >
    > > I'm not sure what you mean by the word "load" in "would load
    > > TestClass?".  "Loading a class" has a meaning different from
    > > "displaying a page".  What the URL does is identify to the app server
    > > (e.g., Tomcat) what page you want to display, or alternatively, what
    > > servlet you want to invoke.  Loading a class is what Tomcat does, or
    > > may do, as part of accessing the specified resource, but is by no
    > > means what the URL signifies.


    You can, for the time being, think of a .war file as a replacement for
    the DLL you'd use with Delphi. In turn, the "actions" are described in
    two places.
    - "Active" actions - those that call server-side logic - are usually
    configured in the war's web.xml descriptor (WEB-INF/web.xml), and
    - "resources" - JSPs that are directly accessible, HTML pages,
    images, etc, are configured by being present somewhere under the root
    of the WAR file.

    The configuration for web.xml should be described in any webapp
    tutorial; there's one at http://www.adp-gmbh.ch/java/web_application/web_xml.html
    ..

    -o
    Owen Jacobson, Oct 31, 2008
    #5
  6. Rich Morey

    Lew Guest

    On Oct 31, 2:23 pm, Rich Morey <> wrote:
    > Hi --
    >
    > Thank you both for your quick replies.
    >
    > > In your example URL (aside: the words "Java" and "Class" in the URL
    > > are bad practice), "javaApp" is the application context and

    >
    > I am aware of this. I just did that for my own learning curve.
    >
    > > This is correct.  However, if your web.xml file isn't set correctly,
    > > this won't load.  Want did you do to configure the web.xml? Can you show us?

    >
    > I should say that I am just learning Java & Tomcat at this point. My
    > web development experience prior to this was with Delphi. When
    > building a Delphi web app a URL would look something like this:
    >
    > http://host/library.dll/action?
    >
    > In this case, the "library.dll" file could/would contain multiple
    > "actions" that could be invoked like this:
    >
    > http://host/library.dll/action1?http://host/library.dll/action2?
    >
    > etc. This would allow me to develop related functionality within a
    > single library. For example, I recently completed a payment processing
    > DLL for a client. In this case the URLs were something like this:
    >
    > http://host/payment.dll/Cash?http://host/payment.dll/Check?http://host/payment.dll/CreditCard?
    >
    > Each action, in this case "Cash", "Check", "CreditCard" was invoked
    > from within the single library, "payment.dll". I would like to
    > replicate this kind of configuration within Java, mostly because this
    > has been the methodology I have used in the past and so I "think"
    > along these lines.


    Tomcat doesn't have DLLs. It has web pages.

    Read
    <http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html>

    --
    Lew
    Lew, Oct 31, 2008
    #6
  7. Rich Morey

    Mark Space Guest

    Rich Morey wrote:

    >
    > I should say that I am just learning Java & Tomcat at this point. My
    > web development experience prior to this was with Delphi. When
    > building a Delphi web app a URL would look something like this:


    Owen seems to have puzzled out the Delphi stuff. I know nothing about
    it. It would still be helpful if you could look at your WEB-INF/
    directory in the .war and show us the contents of web.xml. (See Lew's
    link.)

    If you're just starting out, check out O'Reilly's Head Start Servlets &
    JSP. It goes into web.xml configuration in great detail.

    You also might try this free course. Sang Shin is a Sun researcher who
    gives on-line classes. The JSP course appears to be in progress, but
    the lessons look like they could give you a leg up. The course is
    self-paced and there's no "lecture" so you are really missing anything.

    <http://www.javapassion.com/j2ee/>
    Mark Space, Oct 31, 2008
    #7
  8. Rich Morey

    Juha Laiho Guest

    Rich Morey <> said:
    >I should say that I am just learning Java & Tomcat at this point. My
    >web development experience prior to this was with Delphi. When
    >building a Delphi web app a URL would look something like this:
    >
    >http://host/library.dll/action?
    >
    >In this case, the "library.dll" file could/would contain multiple
    >"actions" that could be invoked like this:
    >
    >http://host/library.dll/action1?
    >http://host/library.dll/action2?


    Yep. As Owen already wrote, you should consider your web application
    (whether packaged as a .war file, or whether just deployed as an open
    directory) as a concept similar to your library.dll on Delphi side.

    Then write servlets for your distinct actions, and you have exactly
    the same calling structure. If you have actions that very much share
    the same code, you could consider making two servlet-mappings for
    a single servlet in your web.xml -- that is, have a single servlet
    available via two action names, and check in the servlet which name
    was used to call it. Or of course, you can use inheritance or
    encapsulation as in normal Java code, and have either one of the
    servlets extend the other, or have a third, possibly a non-servlet
    class, from which the two servlets can access the common functionality.

    You can also, if you want, map a branch of your HTTP resource tree
    to a single servlet, with a servlet-mapping like this:
    <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/Controller/*</url-pattern>
    </servlet-mapping>

    Now, http://server:port/context/Controller/whatever/path/here?with=params
    will be processed by the servlet named "Controller" (mind you, this name
    does not need to bear any relation to the servlet class name).

    Then, of course, you could also call your servlet like
    http://server:port/context/Controller?action=someaction&other=params
    and have your servlet branch the processing based on the value of the
    "action" parameter.

    So, there are a number of ways to provide the processing functionality
    you're looking for. I'd favor the one recommended by Owen, that is,
    switch your thinking so that you consider the web-app as the .dll
    equivalent, and servlets as your actions. Have the servlets
    interpret the parameters, feed the data into appropriate backend classes,
    and set up the result data ready to request context, and forward
    the request to the proper .jsp file for viewing the results to
    the end user. So, you'll have a model (backend classes), view
    (the .jsp file) and a controller (the servlet).
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Oct 31, 2008
    #8
  9. Rich Morey

    Rich Morey Guest


    > The configuration for web.xml should be described in any webapp
    > tutorial; there's one athttp://www.adp-gmbh.ch/java/web_application/web_xml.html
    > .
    >

    Thanks so much! I got this working.

    Rich
    Rich Morey, Oct 31, 2008
    #9
  10. Rich Morey

    Arne Vajhøj Guest

    Rich Morey wrote:
    > I should say that I am just learning Java & Tomcat at this point. My
    > web development experience prior to this was with Delphi. When
    > building a Delphi web app a URL would look something like this:
    >
    > http://host/library.dll/action?
    >
    > In this case, the "library.dll" file could/would contain multiple
    > "actions" that could be invoked like this:
    >
    > http://host/library.dll/action1?
    > http://host/library.dll/action2?
    >
    > etc. This would allow me to develop related functionality within a
    > single library. For example, I recently completed a payment processing
    > DLL for a client. In this case the URLs were something like this:
    >
    > http://host/payment.dll/Cash?
    > http://host/payment.dll/Check?
    > http://host/payment.dll/CreditCard?
    >
    > Each action, in this case "Cash", "Check", "CreditCard" was invoked
    > from within the single library, "payment.dll". I would like to
    > replicate this kind of configuration within Java, mostly because this
    > has been the methodology I have used in the past and so I "think"
    > along these lines.


    I think you would find Struts appealing to you.

    It is a framwork on top of servlets and JSP.

    Arne
    Arne Vajhøj, Nov 1, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Clarke
    Replies:
    1
    Views:
    1,893
    Sudsy
    Jan 7, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    1,996
    Jack Jia
    Apr 4, 2004
  3. Doug McCann
    Replies:
    1
    Views:
    1,005
    William Brogden
    Aug 5, 2004
  4. HS1
    Replies:
    0
    Views:
    455
  5. Stephan Koser
    Replies:
    3
    Views:
    28,399
Loading...

Share This Page