Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to debug a servlet

 
stu ware
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to execute a simple Servlet but I am getting a "Requested Resource Not Available Error".

What I did was,

Installed tomcat 5.5.15 and set the java_home and catalina_home respectively to jdk and tomcat directories.

Developed a html page and dropped it in rootdir/webapps/root/first

Accessed it as, http://localhost:8080/first/index.html

I got the html output. So I assumed tomcat is working fine.

Later I wrote a sample servlet class, by name Hello.java and successfully compiled it. Placed it in rootdir/webapps/root/first/WEB-INF/classes directory.

Later, I wrote the web.xml file and deployed it in

rootdir/webapps/root/first/WEB-INF.

<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hithere</url-pattern>
</servlet-mapping>
</web-app>

I tried to access it as http://localhost:8080/first/hithere

But it was not working and I got a requested resource not available error.

Is there anyway to debug my application. Everything seems to be right. This is driving me crazy.

Any input will be highly appreciated.

Thanks.
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think root is something like a root application to Tomcat. If you put any page i.e. html/jsp in that directory or any other subdirectory of ROOT, it'll be accessible because no mapping is required. Please understand that you've not created a new web application by creating a directory under root. You have to have your directory under webapps and not under root i.e. at the same level as root.

You've also noticed that under root, there is a dir. called web-inf. So, root is a kind of web application and you need to create your own if you want to test anything. You can use root itself but then for deploying such servlets, you need to put the servlet classes under web-inf folder of ROOT and add the entries to the web.xml under that same.

I hope I've solved your problem!
 
stu ware
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I created a directory at the same level as roots under webapps,

like rootdir/webapps/first/index.html

I am unable to access the index.html page as http://localhost:8080/first/index.html

I think http://localhost:8080/ is mapped to rootdir/webapps/root.

Nothing is working. It is so frustrating . Is there any way to findout what is going wrong. I dont think the container is able to read the web.xml file.

Any help is highly appreciated.
 
stu ware
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bimal Patel,

Actually I was wrong. It worked when I placed my directory in webapps. I should have used webapps for my web application.

By the way, how to debug the servlets. Heard about log files. Where can I find them?

Any other techniques to debug......

Thanks for your help.
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Your welcome! I know such kind of troubls while you're in the initial stages of learning servlets/jsp and web application fundamentals.

For debugging servlets, you can do that by remote debugging with any of the IDEs available. I would strongly suggest Eclipse. It is totally free and you can find the help with remote debugging from net. Let me know if I can help much.

You can download Eclipse from here.

[ February 07, 2006: Message edited by: Bimal Patel ]
[ February 07, 2006: Message edited by: Bimal Patel ]
 
Chidanand Chauhan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vasu dupe:
Hi,

Later, I wrote the web.xml file and deployed it in

rootdir/webapps/root/first/WEB-INF.

<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hithere</url-pattern>
</servlet-mapping>
</web-app>

I tried to access it as http://localhost:8080/first/hithere


Hi Vasu I saw your directory structure but i come to the conclussion that your directory structure is wrong.
Create the direcotory structure as i am writting. Please let me know if you have any issue.

Directory Structere:
<% Tomcat-Dir%>/webapps/first/WEB-INF/classes/Hello.java

Now in the web.xml make the entry as you specified in you post.:
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hithere</url-pattern>
</servlet-mapping>
</web-app>


Now deploy and run the tomcat and in the url please write:
http://localhost:8080/first/hithere

you would definatly get the expected output, sure.

cheers,
Chidanand Chauhan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know I sound like a broken record on this, but IMHO all classes used in servlets should be in a package. The directory structure under WEB-INF/classes must reflect the package structure.
Not using a package can result in strange and hard to debug problems.
Older servlet references may not emphasize this. See also the "invoker" servlet FAQ here at the ranch.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic