This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to debug a servlet 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
Bookmark "How to debug a servlet" Watch "How to debug a servlet" New topic
Author

How to debug a servlet

stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
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

Joined: Aug 29, 2003
Posts: 130
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!


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
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

Joined: Feb 04, 2006
Posts: 15
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

Joined: Aug 29, 2003
Posts: 130
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

Joined: Nov 10, 2005
Posts: 77
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

Joined: Mar 22, 2000
Posts: 12678
    
    5

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


Java Resources at www.wbrogden.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to debug a servlet
 
Similar Threads
Having trouble getting Tomcat up and running
sendRedirect Problem
How can I get my HelloWorld Servlet to Run?
Resource not available
Head First Servlet JSP: BeerSelect example: requested resource not available