start working with apache tomcat

i wrote a servlet but i dont know where to put my class file.

i tried to put it under web-inf/class anywhere under the insallation folder but its not working (he cant find the page).

can anyone tell me where exactly i need to put the class file?

im working with apache tomcat 5.5

thanks

[317 byte] By [ppl1a] at [2007-10-2 5:45:05]
# 1

You need to create a directory under 'webapps' that will be the name of your application. Place the class files in WEB-INF/classes under that folder. If I named the folder 'foo' and my Servlet was 'BarServlet', the URL to test would be:

http://localhost:8080/foo/BarServlet

If you place the files in the 'root' (the directory is actually named 'root'), then you do not need to specify the name of your web application in the URL.

- Saish

Saisha at 2007-7-16 1:55:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

its still not working.

here is what i did till now-

i downloaded & install apache tomcat 5.5

i created a simple servlet-

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FirstServlet extends HttpServlet{

public void doGet(HttpServletRequest requset,HttpServletResponse response)

throws ServletException,IOException

{PrintWriter out=response.getWriter();

out.println("done!");

}

}

build it and the class file i put under -instalationDir/webapps/NameOfProject/WEB-INF/classes

and in the browser im writing-

http://localhost:8080/NameOfProject/FirstServlet

and he cant find the page

what am i missing?

thanks in advanced.

ppl1a at 2007-7-16 1:55:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
What is the url-mapping in your web.xml file?- Saish
Saisha at 2007-7-16 1:55:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
you mean the one under conf?
ppl1a at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

You should have a web.xml file for each web app that you declare. any settings you declare in this web.xml file are applicable to that web application only.

in the web.xml file, you should have a <web-app> tag. Under this you will map your servlet like:

<servlet>

<servlet-name>DataServlet</servlet-name>

<servlet-class>DataServlet</servlet-class>

</servlet>

declare all servlet classes likle this, and then start with URL mapping like this:

<servlet-mapping>

<servlet-name>DataServlet</servlet-name>

<url-pattern>/Data</url-pattern>

</servlet-mapping>

So, for each servlet you need to indiocate its .class file and specify the URl mapping.

in the above example, assuming we have a webapp "foo" under webapps folder,

the web.xml file should be in the folder:

<Tomcat installation dir>\webapps\Mtool\WEB-INF

and the url to access it is:

http://localhost:8080/foo/Data

Hope this helps

maxmudita at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi friend,The method will solve your issue or you can also call the servletwith method which was suggested by saish earlier but by using following URL ..... http://localhost:8080/examples/servlet/BarServletHope this would work for
RahulSharnaa at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

thats my web.xml-

?xml version="1.0" encoding="ISO-8859-1"?>

<web-app>

<servlet>

<servlet-name>FirstServlet</servlet-name>

<servlet-class>ServletTry</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FirstServlet</servlet-name>

<url-pattern>/FirstServlet</url-pattern>

</servlet-mapping>

i put it under directory webapps/ServletTry

i guess its wrong...

</web-app>

ppl1a at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Okay. Look at your url-pattern, that is what must go after the web app name. So, if you have a folder structure that looks like this:

Tomcat Root

webapps

ServletTry

WEB-INF

classes

FirstServlet.class

And your url-mapping is 'FirstServlet', then the URL you submit is:

http://localhost:8080/ServletTry/FirstServlet

- Saish

Saisha at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

<<servlet-class>ServletTry</servlet-class> >

actually he has indicated "ServletTry" as his class in the web.xml file.

ppl1, you should indicate the name of the .class file for your servlet in the tags for <servlet-class>

and the name by which you want your servlet to be referred to in:

<url-pattern> tag.

Under the webapps folder, you define your individual web applications as separate folders. You should create a folder with the name you want your web application to be known as under webapps folder.

maxmudita at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

its looking like that-

tomcat 5.5

webapps

- ServletTry

-- WEB-INF

--classes

-FirstServlet.class

my web.xml is-

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app>

<servlet>

<servlet-name>FirstServlet</servlet-name>

<servlet-class>FirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FirstServlet</servlet-name>

<url-pattern>/FirstServlet</url-pattern>

</servlet-mapping>

</web-app>

i put it under directory ServletTry.

in the browser i wrote-

http://localhost:8080/ServeltTry/FirstServlet

its not working.... :(

thatnks for the help

ppl1a at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

here the problem is you didn't wrote the web.xml file.

i am writing here

<webapp>

<servlet>

<servlet-name>FirstServlet </servlet-name>

<servlet-class>FirstServlet </servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FirstServlet ></servlet-name>

<url-pattern>/myservlet<url-pattern>

</servlet-mapping>

</webapp>

copy this file and save with name wex.xml and put this one in the under WEB-INF directory.

and then restart the server

now you type the url in the address bar as

http://localhost:80880/foo/myservlet.

i think it will work mostly.

ok bye,

babu.

_123456a at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
its not working...
ppl1a at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
servlet must be in a package
declangallaghera at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
still not working
ppl1a at 2007-7-16 1:55:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

If your folder structure is like this:

tomcat 5.5

webapps

- ServletTry

-- WEB-INF

--classes

-FirstServlet.class

then your web.xml file should be in the path:

tomcat 5.5\webapps\ServletTry\WEB-INF\

after restarting, try this link:

http://localhost:8080/ServeltTry/FirstServlet

if it still does not work, please post the error message that you get

and after restarting the server, you can access the servlet using:

maxmudita at 2007-7-20 18:41:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
thats what i did and i got the "The page cannot be dispayed" messagein the bottom it is written-"Cannot find server or DNS ErrorInternet Explorer"thanks for the help.
ppl1a at 2007-7-20 18:41:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17

Hi there!

I had also big problems to understand the structure and functions in Tomcat.

Uninstall your Apache Tomcat and install again, the go to this website:

http://www.coreservlets.com/Apache-Tomcat-Tutorial/#Test1

there are two things to do

1. make a directory, in <inst dir>\Tomcat 5.5\webapps\ROOT\WEB-INF\

classes

2. replace your original web.xml with his/hers modified web.xml.

And now you should be set to go!

Thanks to that great tutorial, Apache Tomcat was much easier to understand!

// LiPpE

LiPpEa at 2007-7-20 18:41:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...