• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing my first servlet program

 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,
I am trying to learn j2ee, servlets and Ejb. i am having a nightmare in running my first servlet program. Here is how my TestingServler looks like. here is the directory where i stored my .java file
C:\jakarta-tomcat-5.0.14\webapps\myApp\WEB-INF\classes


and here is my web.xml file which i have placed in
C:\jakarta-tomcat-5.0.14\webapps\myApp\WEB-INF



and here is my classpath for servlet-api.jar file

.;C:\Program Files\QuickTime\QTSystem\QTJava.zip;C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;C:\Java programs\ir.jar

and this is what i am typing after i start tomcat5.0

http://localhost:8080/myApp/servlet/Testing
or
http://localhost:8080/myApp/servlet/TestingServlet

None of them works and give me an error



Any help will be really appreciated.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the directory where i stored my .java file

1. You mean the .class file, right ? You have compiled the servlet and put the .class file here, right ? Don't put java files in your classes directory.
2. You'd better put your servlet in a package, instead of putting it right under classes.
3. You need to define a <servlet-mapping> tag to map a url to your servlet. Try it, and if you still don't understand how to define it, come back
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be a <servlet-mapping> element in your web.xml
Regards,
Srikkanth.M
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satou thanks for the reply. after hours of debugging i have left my laptop alone and will do it tomorrow morning. I was just following the steps in the book and they state to put java files in the classes directory. Lets see if i can put it in a package and then run the servlet file.

I will post tomorrow.

Thanks
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book cost me good $42
java for web servers with Ejb, Jsp. etc...

There was no servlet-mapping in the xml file but i will try to figure it out tomorrow if not then i will post.

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like that book is outdated.
These days, most webservers require you to explicitly map your servlets.

Tocmat used to have an invoker servlet enable by default which prevented the need for this but there are countless problems with using it and it has been deprecated.
See: http://faq.javaranch.com/view?InvokerServlet
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Here i am back again. Looks like i had to bug myself for a long time. I did uncomment the lines as stated in the url(invoker servlet) and i have added the mapping also for the servlet, but still it is not compiling



type Status report

message /myApp/Testing

description The requested resource (/myApp/Testing) is not available.
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok glad i am able to run my first servlet program after hours and hours of debugging. OMG i hate authors of some books.

ANyways one more question and i guess i am all set to go to learn about j2ee . Excited and happy. I was wondering if i created an xml file web.xml just for that specific TestingServlet.java file where do i save it and how do i run it.

TestingServlet.java(C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myApp\WEB-INF\classes\chapter1)

web.xml(C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myApp\WEB-INF\classes\chapter1)

This works fine
http://localhost:8080/myApp/servlet/chapter1.TestingServlet

But this does not work
http://localhost:8080/myApp/servlet/chapter1.Testing

I am trying to invoke the servlet through my web.xml file and this is how my web.xml file looks like.



if i include mapping it looks like this


i tried both but no success.
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

It's in chapter 1 of your book:
Step 4: Create the Deployment Descriptor

Herman
PS: if you use Tomcat 5, it's easier to use a book that also uses Tomcat 5.
Your book uses Tomcat 4, and somethings just work differently...
[ May 25, 2007: Message edited by: Herman Scheltinga ]
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Herman yes you are right. Step4 will teach you about the deplyment descriptor but its not working. I dont know why. any idea?
 
Suleman zia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG It worked. Yayyyyy.

took me 9 hours to run this simple program oh well lets see what happens when it comes to JBoss and JDBC.

Thanks
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

what did you do to make it work?

I hope you did this(from your book)
"To create the deployment descriptor, you now need to create a web.xml file and place it under the WEB-INF directory under myApp."

Herman
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic