Java/TomCat How to Configure a Servlet contextPath

R

Rich Morey

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
 
L

Lew

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.
 
R

Rich Morey

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
 
O

Owen Jacobson

Hi --

Thank you both for your quick replies.


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


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

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
 
L

Lew

Hi --

Thank you both for your quick replies.


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


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>
 
M

Mark Space

Rich 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:

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/>
 
J

Juha Laiho

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).
 
A

Arne Vajhøj

Rich 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?

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top