permaculture playing cards*
The moose likes Servlets and the fly likes how to use the servlet in tomcat?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

how to use the servlet in tomcat??

Tony Ge
Greenhorn

Joined: Sep 05, 2001
Posts: 20
if the test.java were a servlet and can be browsen in :
http://localhost:8080/hello/test
hello is the directory under :tomcat_home/webapps/root
where should I put the test.java in ??
or how i sould modify the tomcat's servlert config??
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
Hi,
Assuming that you are new to tomcat. I am about to give you solution.
1. place the test servlet inside classes folder found in \ROOT\WEB-INF\classes(if your test servlet is having a package, comment it for testing).
2. call this test servlet
http://localhost:8080/root/servlet/test
This should work.

But you better creat a context similar to root. For running your servlet.To achive that do the following steps.
1 .create a folder of any name inside "webapps "folder of tomcat.
2. map this in server.xml found in <tomcat_home>/conf folder(i.e, <Context path="/examples" docBase="webapps/examples" crossContext="false" debug="0" reloadable="true"/> copy this tag inside this file and replace "examples" by the folder name you have created)
3. see that folder created contains web-inf and classes folder and web.xml file similar to root context (make a copy of web.xml from root context if you want).
4. Place your files (servlet ,html, jsp )and restart.
5.See that your servlet and helper class are placed inside classes folder always.
6. refer your context by url given below
http:\\localhost:8080\yourfolder\.....

-arun


Sharing is learning
Tony Ge
Greenhorn

Joined: Sep 05, 2001
Posts: 20
test successfully!!!
much thanx!!!
Oleksandr Karpus
Greenhorn

Joined: Feb 13, 2002
Posts: 10
Hi Arun Boraiah
It does not help me!
I follow your or M.Hall instructions
"If you get compilation errors, go back and check your CLASSPATH settings (see the earlier section on this topic)--you most likely erred in listing the location of the JAR file that contains the servlet classes (i.e.,install_dir/common/lib/servlet.jar). Once you compile HelloServlet.java, put HelloServlet.class in install_dir/webapps/ROOT/WEB-INF/classes. After compiling the code, access the servlet with the URL http://localhost/servlet/HelloServlet (or http://localhost:8080/servlet/HelloServlet or
http://127.0.0.1:8080/root/servlet/helloServlet if you chose not to change the port number as described earlier). You should get a simple HTML page that says "Hello". If this URL fails but the test of the server itself succeeded, you probably put the class file in the wrong directory. "
My HelloServlet.java is in C:\Program Files\Apache Tomcat 4.0\webapps\ROOT\WEB-INF\classes. I compile HelloServlet.java from TextPad.
Always have reply like:
"Apache Tomket/4.0.2 - HTTP Status 404
-/root/servlet/helloServlet
type Status report
message /root/servlet/helloServlet
description The requested resource (/root/servlet/helloServlet ) is not available."
If call it http://localhost:8080/root/servlet/helloservlet
have: An error has occurred processing the request...
Sasha
SCJP2
( Java is my hobby )


O.Karpus<br />SCJP2, SCWD
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

take out the word 'root'
pari pat
Greenhorn

Joined: Feb 15, 2002
Posts: 5
Just wanna find the same
Oleksandr Karpus
Greenhorn

Joined: Feb 13, 2002
Posts: 10
It's OK now.
Thanks Mike Curwen
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello Arun
I followed ur instructions but was not successful
But you better creat a context similar to root. For running your servlet.To achive that do the following steps.
1 .create a folder of any name inside "webapps "folder of tomcat.
--created "shan"--
2. map this in server.xml found in <tomcat_home>/conf folder(i.e, <Context path="/examples" docBase="webapps/examples" crossContext="false" debug="0" reloadable="true"/> copy this tag inside this file and replace "examples" by the folder name you have created)
I did'nt find as above
<!-- Tomcat Examples Context -->
<Context path="/examples" docBase="examples" debug="0"
reloadable="true">
I changed examples to "shan"

3. see that folder created contains web-inf and classes folder and web.xml file similar to root context (make a copy of web.xml from root context if you want).

webapps
|
|shan
|
|WEB-INF contains classes and web.xml file
4. Place your files (servlet ,html, jsp )and restart.
"done"
5.See that your servlet and helper class are placed inside classes folder always.
"done"
6. refer your context by url given below
http:\\localhost:8080\yourfolder\.....
http:\\localhost:8080\shan\test

I get this error
type Status report
message /shan/test
description The requested resource (/shan/test ) is not available."
what is the problem???
[ February 20, 2002: Message edited by: shan java ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Got it!!!
Tony Ge
Greenhorn

Joined: Sep 05, 2001
Posts: 20
may be you can delete the web.xml in the folder you created!!
try !!!
krish chandru
Ranch Hand

Joined: Apr 27, 2001
Posts: 39
Hi
i want to write a servlet that just does initialisation work. It is not called by any other page, but it is directly invoked in the url and the application flows subsequently. i presume i should not write the initialisation code in doGet or doPost. am i right ? if yes how else can i do it ?
krish chandru
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
Servlet initialization belongs in the init() method. I don't understand what you mean by
It is not called by any other page, but it is directly invoked in the url and the application flows subsequently.

Are you going to direct requests to this servlet or not? You can certainly make a servlet that gets loaded on startup and you can control the order in which the startup servlets get initialized.
Bill
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

By my count, this is the fourth time this thread has been re-used, for a different question.

So I'm closing it now. If anyone wants to continue a topic, start your own thread. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use the servlet in tomcat??
 
Similar Threads
How container recognizes a request for servlet
Servlet is running after tomcat is stopped.
Servlet Basic Error !!
problem with mapping using servletrunner
problem in running a servlet