I am new to JavaRanch. Hopefully I have posted this in correct forum. If not, I am sorry.
I have written a simple servlet 'Ch1Servlet.java' to print current date and time in browser. I am using Apache Tomcat. My directory structure is like this:
'web.xml' is kept in directory 'WEB-INF', 'Ch1Servlet.class' is kept in directory 'classes'.
When I enter http://localhost/ch1/Serv1 in browser it shows date and time. This is what I wanted. However when I change the directory structure as follows:
description The requested resource (/anurag/ch1/Serv1) is not available.
Apache Tomcat/6.0.18
What's the reason? Is it due to 'web.xml' is too deeper and is not being read? What changes I have to make to my ditectory structure and/or to my 'web.xml' file so that I can enter http://localhost/anurag/ch1/Serv1 to get desired result.
Bear Bibeault wrote:Step 1: move the servlet into a package other than the default.
Thank you. I will try that.
Anurag Patil
Greenhorn
Joined: Mar 10, 2009
Posts: 9
posted
0
Bear Bibeault wrote:Step 1: move the servlet into a package other than the default.
Well, I tried that. But still no success. I think I got you wrong. I am making some mistake, but I don't know where. What I did was I created package com/example/web within 'ch1'. My new structure is as follows:
tomcat
|
webapps
|
anurag
|
ch1
|
WEB-INF -> web.xml
|
classes
|
com
|
example
|
web -> Ch1Servlet.class
The string that you specify in the browser should be the same that appears in the url-pattern.
I hope it helps.
Kindly inform me if i have written something incorrect. Thanks.
SCJP 5.0, SCWCD 5.0, SCBCD 5.0
Kuldeep Tewari
Ranch Hand
Joined: May 22, 2006
Posts: 35
posted
0
Can you also check if your application is actually deployed on the server(a directory with name 'anurag' under TOMCAT_HOME/work). I suspect, your application is not correctly structured.
Keep this structure :
anurag
|
WEB-INF -> web.xml
|
classes
|
com
|
example
|
web -> Ch1Servlet.class
ryan sukale wrote:Hi Anurag,
Now that you have moved the class file to the appropriate directory, you need to understand the <url-pattern> tag correctly.
the url pattern in your web.xml file is : /Serv1
The backslash represents the application's root directory. In this case, your root directory is : anurag
The string that you specify in the browser should be the same that appears in the url-pattern.
I hope it helps.
Kindly inform me if i have written something incorrect. Thanks.
Hi Ryan,
Thanks for your help. But... Still no success... As you have asked, I entered http://localhost/anurag/Serv1 in browser. It is showing HTTP Status 404 error:The requested resource (/anurag/Serv1) is not available. I also tried several combinations of <servlet-class> in web.xml by keeping <url-pattern>/Serv1</url-pattern> same such as <servlet-class>com.example.web.Ch1Servlet</servlet-class>, <servlet-class>ch1.com.example.web.Ch1Servlet</servlet-class>, <servlet-class>anurag.ch1.com.example.web.Ch1Servlet</servlet-class>
Thank you.
Anurag
Anurag Patil
Greenhorn
Joined: Mar 10, 2009
Posts: 9
posted
0
K Tewari wrote:Can you also check if your application is actually deployed on the server(a directory with name 'anurag' under TOMCAT_HOME/work). I suspect, your application is not correctly structured.
Keep this structure :
anurag
|
WEB-INF -> web.xml
|
classes
|
com
|
example
|
web -> Ch1Servlet.class
Thanks for your help. It works. In my first post I have mentioned that. But, what I want is different. I want directory structure to be the same as I mentioned below and want to get the result.
Even if you dont need all the folders mentioned above, if you ever need them, they must be strictly in the following hierarchy. This ensures that the web application is easily deployed across different containers. This is a standard way of packaging web applications built in java and is mandated by the specification.
I hope this helps.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
Expect of the /src folder.
Most IDE's use it to place the Java sources in. But it is not part of the structure as specified in the Servlet API.