This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Running Servlets, have a question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running Servlets, have a question" Watch "Running Servlets, have a question" New topic
Author

Running Servlets, have a question

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

The WebServer I am using is Tomcat5.5.I compiled my Servlet program and now I want to run it. I took the .class file and put it in C:\tomcat\apache-tomcat-5.5.26\webapps\ROOT\WEB-INF\Classes folder and started my Tomcat server.

Now when I do this,
http://localhost:8080/ it's taking me to the tomcat administration website or something. But when I do this, http://localhost:8080/servlet/HelloWorld I am getting an error as follows:
HTTP Status 404 - /servlet/HelloWorld.

My questions are,

1).Am I doing anything wrong in running the servlet program?

2).Also, do we have to manually copy the .class file every time we compile our program to the tomcat classes folder?

3). How do I login to the Tomcat manager on http://localhost:8080/. It's asking me an Id and Pwd which I haven't choose at all. Is there a default Id and Pwd?

Thank you.


Be Humble... Be Nice.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1) It looks like you are trying to use the InvokerServlet, which I think is turned off by default. Check this FAQ. You need to uncomment to things in Tomcat's default web.xml

2) Yes, unless you think of something to automatically put them there for you. A batch file. Or even setting the output directory at compile time.

3) Check tomcat-users.xml in the Tomcat's conf directory


[My Blog]
All roads lead to JavaRanch
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I can not find the invoker servlet in the web.xml file in the WEB-INF folder.

Thanks
[ April 01, 2008: Message edited by: Arjun Reddy ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Instead of enabling the Invoker servlet (which was deprecated for the reasons listed in the link provided for by Christophe), why don't you create a mapping for your servlet?

If you want an example that's really quick and easy to get running, go to our CodeBarn and download the SimpleServlet war file. As soon as you drop it in your webapps directory, Tomcat will unpack and deploy it for you.
Then, you will have a properly laid out web application with a deployment descriptor that has correct mappings.

See:
http://faq.javaranch.com/java/CodeBarnSimpleServlet


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Instead of relying on the *default* setup, having your own web application would be better for dealing with even simple servlets and jsps.

That's because as Ben suggested, you have a *better control* over it!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I just have a few more questions. My current web.xml looks like this:



Should I be adding another <servlet> and <servlet-mapping> now? I tried adding the below part to what is already existing:

<servlet>
<servlet-name>HelloWorld</servlet-name> HelloWorld is the java file name
<servlet-class>Users.Ravi.Desktop.CurrentProject.Servlets.HelloWorld</servlet-class>
</servlet> Here I gave the path to the place where the .class file is, this is not the path of the WEB-INF classes.

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>


Now, did I do anything wrong here, as Both seem to not do anything except for giving the 404 error again
HelloWorld[/b]]http://localhost:8080/HelloWorld or
hello[/b]]http://localhost:8080/hello

I've also tried giving the servlet-class the path of the class name in the web-inf dir but it still did not work.

Any suggestions please?/
[ April 01, 2008: Message edited by: Arjun Reddy ]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Anybody?/
Sudhakar Duraiswamy
Ranch Hand

Joined: Dec 20, 2007
Posts: 43
Hi arjun,
The default username and pwd for TOMCAT would be 'admin' and 'admin'.
If this doesnt work try giving username alone.


As far as the browser not finding the Servlet .
The servlet-class should be pointing to the class name along with the package

for example





in web.xml



[ April 02, 2008: Message edited by: Sudhakar Duraiswamy ]



Sudhakar.D
[ April 02, 2008: Message edited by: Sudhakar Duraiswamy ]

Sudhakar
SCJP 5.0 93%| SCWCD (prep. ongoing))
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

Thanks for your reply.I have done that before itself but it's still not working?/
suri jagadish
Greenhorn

Joined: Sep 14, 2005
Posts: 19
Hi Arjun,

You are right with regards to the mapping in the web.xml.

<servlet>
<servlet-name>HelloWorld</servlet-name> HelloWorld is the java file name
<servlet-class>Users.Ravi.Desktop.CurrentProject.Servlets.HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

but the only problem is url which you are using to call the servlet. it should be in the following way

http://localhost:8080/HelloWorld/hello

in the above URL you have different parts which are necessary.

http://<servername>:<port>/<context name>/<url-pattern or the name of the jsp>

The context name is the name of the folder of your application in the webapps directory of the tomcat.
Andrew Han
Greenhorn

Joined: Aug 22, 2007
Posts: 4
Originally posted by suri jagadish:
Hi Arjun,

You are right with regards to the mapping in the web.xml.

<servlet>
<servlet-name>HelloWorld</servlet-name> HelloWorld is the java file name
<servlet-class>Users.Ravi.Desktop.CurrentProject.Servlets.HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

but the only problem is url which you are using to call the servlet. it should be in the following way

http://localhost:8080/HelloWorld/hello

in the above URL you have different parts which are necessary.

http://<servername>:<port>/<context name>/<url-pattern or the name of the jsp>

The context name is the name of the folder of your application in the webapps directory of the tomcat.


it should be in the following way:
http://localhost:8080/ROOT/hello
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I tried that, http://localhost:8080/ROOT/hello and it's giving me this,

HTTP Status 404 - /ROOT/hello

type Status report

message /ROOT/hello

description The requested resource (/ROOT/hello) is not available.
Apache Tomcat/5.5.26



Only this is working as of now. http://localhost:8080
Ajith Prasad
Ranch Hand

Joined: Aug 22, 2006
Posts: 66
did you try
http://localhost:8080/hello
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I just did and it's giving me a different error message now:


HTTP Status 404 - Servlet HelloWorld is not available

type Status report

message Servlet HelloWorld is not available

description The requested resource (Servlet HelloWorld is not available) is not available.
Apache Tomcat/5.5.26

[ April 04, 2008: Message edited by: Arjun Reddy ]
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
Arjun,

Where exactly is your HelloWorld.class file located on disk? Is it physically in the "..\Users\Ravi\Desktop\CurrentProject\Servlets" directory? If it's not actually within the your application's WEB-INF folder this is not going to work.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi Scott,

I put the HelloWorld.class file which was in C:\Users\Ravi\Desktop\CurrentProject\Servlets into

C:\Tomcat\apache-tomcat-5.5.26\webapps\ROOT\WEB-INF.

I just checked tho, there are some 9 WEB-INF folders in the TOMCAT folder and I wonder if ROOT\WEB-INF is the one in which I have to put the HelloWorld.class file and update the web.xml file in that directory.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

Originally posted by Arjun Reddy:
C:\Tomcat\apache-tomcat-5.5.26\webapps\ROOT\WEB-INF
Incorrect. The class hiearchy needs to be rooted at WEB-INF/classes and reflect the package structure of the classes.

I just checked tho, there are some 9 WEB-INF folders
Each web app will have its own WEB-INF folder. You need to find out which application you're trying to update.

"tho" is not a real word. Please use only real words like "though" when posting to the forums.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I am sorry about that and I really cannot understand what you have said. I am a beginner so, If you don't mind, Can you please explain what you have said before?

Are you saying the HelloWorld.class file should be in
C:\Tomcat\apache-tomcat-5.5.26\webapps\ROOT\WEB-INF\Classes. If so, Should I still be typing in http://localhost:8080/ROOT/hello in the browser?
[ April 05, 2008: Message edited by: Arjun Reddy ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61104
    
  66

Do you know Java? Do you understand packages? If not, that's knowledge you'll need before diving off into servlets.
kamalkant rajput
Greenhorn

Joined: Nov 26, 2007
Posts: 22
hi arjun,
you make new folder in webapps under that create WEB-INF/classes and put your web.xml file along with classes folder in web.xml you write your servlet mapping. under classes folder you put your .class files.
suppose you have create hello folder in webappps and your url name is /Hello for your servlet then start the server and put localhost:8080/hello/Hello as url if you have not changed port number.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running Servlets, have a question