aspose file tools*
The moose likes Servlets and the fly likes Servlets with Tomcat 4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets with Tomcat 4" Watch "Servlets with Tomcat 4" New topic
Author

Servlets with Tomcat 4

Ria Mathur
Greenhorn

Joined: Oct 11, 2001
Posts: 14
I have recently installed Tomcat on my win 2k machine. I have set the necessary env vaiables and I am able to run the sample servlets successfully. How can I run my own servlets here? Where do I put them? I have made a dir "TEST" under webapps and put my servlet class files there, but no luck. Do I need to write some XML files here? I know nothing abt XML, how can I run my servlets using tomcat?
Also, is it true that tomcat is not used for commercial purpose? Can I run servlets with apache?
Thanks,
- Ria


-Ria
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

To run 'your own' servlets you need to put them in 'webapps/<YOUR WEB APPLICATION>/WEB-INF/classes' and configure web.xml in 'webapps/<YOUR WEB APPLICATION>/WEB-INF' (if you go to 'webapps/ROOT/WEB-INF' you can copy web.xml from there). In web.xml you should enter these lines inside the <web-app> tag:

You can now call the servlet like this:
http://127.0.0.1/<YOUR WEB APPLICATION>/test
/Rene
[ July 10, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
ofcourse as long as you have placed them in the classes/ directory, you should also be able to get them via
http://localhost:8080/yourwebapp/servlet/your.servlet.name
in the default setup


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
If you define your own context, it must also be defined in the $catalina_home/conf/server.xml
as <Context path="/yourWebapp" docBase="yourWebapp" debug="0" reloadable="0"/> for the request URL to work.
Ria Mathur
Greenhorn

Joined: Oct 11, 2001
Posts: 14
Thank you everyone, it works now.
Can someone explain the tomcat and apache conflict?
- Ria
bernd pier
Ranch Hand

Joined: Jun 21, 2002
Posts: 30
hey,
i had a similary problem and the comments to this article had help me to solve it, but i didn`t understand, why in the <servlet-class> tag stand com.sun.TestServlet. i`ve wrote the class name of my servlet and that�s ok.
is there a difference between com.sun.TestServlet and my solution? :roll:
thanks
bernd
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

It was just a sample for showing e.g. the package as well
/Rene
[ July 11, 2002: Message edited by: Rene Larsen ]
Howard Ralston
Ranch Hand

Joined: Jun 25, 2001
Posts: 105
Hi guys,
I am trying to run a sample servlet from "Java Servlets Developer's Guide". I tried to set up my own web application called myServlets in the following path: E:\Tomcat\jakarta-tomcat-4.0.3\webapps\myServlets.
I installed the servlet called WebAppProperties in the WEB-INF folder in this path: E:\Tomcat\jakarta-tomcat-4.0.3\webapps\myServlets\WEB-INF\classes\com\omh\webapps.
The following is my web.xml file located in the WEB-INF folder:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>maxThings</param-name>
<param-value>100</param-value>
</context-param>
<context-param>
<param-name>Author</param-name>
<param-value>Howard Ralston</param-value>
</context-param>
<servlet>
<servlet-name>WebAppProperties</servlet-name>
<servlet-class>com.omh.webapps.WebAppProperties</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebAppProperties</servlet-name>
<url-pattern>/WebAppProperties</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Properties</servlet-name>
<servlet-class>com.omh.webapps.Properties</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Properties</servlet-name>
<url-pattern>/Properties</url-pattern>
</servlet-mapping>
</web-app>
When I try to run the servlet using "http://localhost:8080/myServlets/servlet/com.omh.webapps.WebAppProperties", Tomcat gives me an error message saying that the requested resource is not available.

Any ideas as to what else to do to get this to work?
Thanks,
Howard
[ July 15, 2002: Message edited by: Howard Ralston ]

<a href="http://www.getlocaldeals.com" target="_blank" rel="nofollow">Free local coupons</a>
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

The web.xml is not configured correct. I think you also have some error in one of the log files in '<tomcat_install>/logs/'.
All the <servlet> tags MUST be defined before the <servlet-mapping> tags.
/Rene
Howard Ralston
Ranch Hand

Joined: Jun 25, 2001
Posts: 105
Thanks Rene,
I changed the web.xml file to:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>maxThings</param-name>
<param-value>100</param-value>
</context-param>
<context-param>
<param-name>Author</param-name>
<param-value>Howard Ralston</param-value>
</context-param>
<servlet>
<servlet-name>WebAppProperties</servlet-name>
<servlet-class>com.omh.webapps.WebAppProperties</servlet-class>
</servlet>
<servlet>
<servlet-name>Properties</servlet-name>
<servlet-class>com.omh.webapps.Properties</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebAppProperties</servlet-name>
<url-pattern>/WebAppProperties</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Properties</servlet-name>
<url-pattern>/Properties</url-pattern>
</servlet-mapping>
</web-app>
However, it still doesn't work. Where do I find the '<tomcat_install>/logs/'you mentioned above, and what should I look for?
Thanks,
Howard
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I think the name of the log file in tomcat 4.0.3 is: 'localhost_log.<date>.txt'. In this file you can see if all web applications are loaded correct or an error has occured.
The file is in the folder 'logs' on the same level as the folder 'webapps' witch contains all web applications by default.
Howard Ralston
Ranch Hand

Joined: Jun 25, 2001
Posts: 105
Rene,
I found the log file, and there is an error. As it is lengthy, I will only post the portion of the file that deals with the folder "myServlets". Please let me know how to correct the error.
Thanks,
Howard
2002-07-16 09:25:29 WebappLoader[/myServlets]: Deploying class repositories to work directory E:\Tomcat\jakarta-tomcat-4.0.3\work\localhost\myServlets
2002-07-16 09:25:29 StandardManager[/myServlets]: Seeding random number generator class java.security.SecureRandom
2002-07-16 09:25:29 StandardManager[/myServlets]: Seeding of random number generator has been completed
2002-07-16 09:25:30 ContextConfig[/myServlets] Parse error in application web.xml
java.lang.NullPointerException
at org.apache.catalina.util.xml.XmlMapper.readXml(XmlMapper.java:295)
at org.apache.catalina.startup.ContextConfig.applicationConfig(ContextConfig.java:256)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:824)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:224)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:155)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1131)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3345)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:614)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1123)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:343)
at org.apache.catalina.core.StandardService.start(StandardService.java:388)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:506)
at org.apache.catalina.startup.Catalina.start(Catalina.java:781)
at org.apache.catalina.startup.Catalina.execute(Catalina.java:681)
at org.apache.catalina.startup.Catalina.process(Catalina.java:179)
at java.lang.reflect.Method.invoke(Native Method)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:243)
2002-07-16 09:25:30 ContextConfig[/myServlets]: Marking this application unavailable due to previous error(s)
2002-07-16 09:25:30 StandardContext[/myServlets]: Error initializing naming context for context /myServlets
2002-07-16 09:25:30 StandardContext[/myServlets]: Context startup failed due to previous errors
2002-07-16 09:25:30 StandardContext[/myServlets]: Error initializing naming context for context /myServlets
Howard Ralston
Ranch Hand

Joined: Jun 25, 2001
Posts: 105
Rene,
Thanks for the help! The problem was that after correcting the web.xml file, I needed to restart Tomcat. I have done so and the servlet works perfectly.
Thanks again,
Howard
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Why don't you give this a try:

or

Since you have defined a mapping to these url patterns to your serlvets in your deployment descriptor.
craig
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Sorry, or this url:
web page
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
One more time:
http://localhost:8080/mySerlvets/Properties
Sanjeev Shahi
Ranch Hand

Joined: Feb 26, 2001
Posts: 83
how to run sample servelets
?
ss
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets with Tomcat 4