Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java/TomCat How to Configure a Servlet contextPath

Reply
Thread Tools

Java/TomCat How to Configure a Servlet contextPath

 
 
Rich Morey
Guest
Posts: n/a
 
      10-31-2008
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
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      10-31-2008
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?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      10-31-2008
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
 
Reply With Quote
 
Rich Morey
Guest
Posts: n/a
 
      10-31-2008
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


 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      10-31-2008
On Oct 31, 2:23*pm, Rich Morey <(E-Mail Removed)> 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...y.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://...ll/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_appl...n/web_xml.html
..

-o
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-31-2008
On Oct 31, 2:23*pm, Rich Morey <(E-Mail Removed)> 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...y.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://...ll/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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      10-31-2008
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/>

 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      10-31-2008
Rich Morey <(E-Mail Removed)> 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:razz:ort/context/Contr...re?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:razz:ort/context/Contr...n&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)
 
Reply With Quote
 
Rich Morey
Guest
Posts: n/a
 
      10-31-2008

> 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
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      11-01-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to get the contextPath in a servlet's init-method? Stephan Koser Java 3 01-15-2005 07:16 AM
different between configure libraries and configure JDKs HS1 Java 0 11-18-2004 03:40 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
how to get my servlet configuration before the servlet is initialised Andy Fish Java 4 12-17-2003 09:47 PM



Advertisments