This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Deployment Environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Deployment Environment" Watch "Deployment Environment" New topic
Author

Deployment Environment

Anurag Patil
Greenhorn

Joined: Mar 10, 2009
Posts: 9
Hi,

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:

tomcat
|
webapps
|
ch1
|
WEB-INF -> web.xml
|
classes -> Ch1Servlet.class

'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:

tomcat
|
webapps
|
anurag
|
ch1
|
WEB-INF -> web.xml
|
classes -> Ch1Servlet.class


and enter http://localhost/anurag/ch1/Serv1 in the browser it shows following error:

HTTP Status 404 - /anurag/ch1/Serv1

type Status report

message /anurag/ch1/Serv1

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.

My 'web.xml' is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>Chapter1 Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Chapter1 Servlet</servlet-name>
<url-pattern>/Serv1</url-pattern>
</servlet-mapping>

</web-app>


Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Step 1: move the servlet into a package other than the default.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anurag Patil
Greenhorn

Joined: Mar 10, 2009
Posts: 9
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
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


Here is my servlet Ch1Servlet.java

package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Ch1Servlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out = response.getWriter();

java.util.Date today = new java.util.Date();

out.println("<html>"+"<body>"+today+"</body>"+"</html>");
}
}

and My web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>Chapter1 Servlet</servlet-name>
<servlet-class>com.example.web.Ch1Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Chapter1 Servlet</servlet-name>
<url-pattern>/Serv1</url-pattern>
</servlet-mapping>

</web-app>


Thank you.
ryan sukale
Ranch Hand

Joined: Aug 11, 2007
Posts: 53
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

so in your browser window, type the following : http://localhost/anurag/Serv1

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
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

and then try this url : http://localhost:<<your_port>>/anurag/Serv1
Anurag Patil
Greenhorn

Joined: Mar 10, 2009
Posts: 9
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

so in your browser window, type the following : http://localhost/anurag/Serv1

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
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

and then try this url : http://localhost:<<your_port>>/anurag/Serv1

Hi K Tewari,

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.

tomcat
|
webapps
|
anurag
|
ch1
|
WEB-INF -> web.xml
|
classes -> Ch1Servlet.class


or

tomcat
|
webapps
|
anurag
|
ch1
|
WEB-INF -> web.xml
|
classes
|
com
|
example
|
web -> Ch1Servlet.class


Is it not possible with the directory structure I mentioned?

Thank you.

Anurag
ryan sukale
Ranch Hand

Joined: Aug 11, 2007
Posts: 53
Hi Anurag
A web application has to have the following sructure

1. myapp.
2. myapp/WEB-INF.
3. myapp/WEB-INF/web.xml.
4. myapp/WEB-INF/src.
5. myapp/WEB-INF/classes
6. myapp/WEB-INF/lib.

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
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.
Anurag Patil
Greenhorn

Joined: Mar 10, 2009
Posts: 9
Thank you Ryan & Bauke. So what I understand is that there CAN NOT be structure like:

1. tomcat
2. tomcat/webapps
3. tomcat/webapps/anurag
4. tomcat/webapps/anurag/ch1
5. tomcat/webapps/anurag/ch1/WEB-INF
6. tomcat/webapps/anurag/ch1/WEB-INF/web.xml
7. tomcat/webapps/anurag/ch1/WEB-INF/src
8. tomcat/webapps/anurag/ch1/WEB-INF/classes
9. tomcat/webapps/anurag/ch1/WEB-INF/lib


It has to be:

1. tomcat
2. tomcat/webapps
3. tomcat/webapps/ch1
4. tomcat/webapps/ch1/WEB-INF
5. tomcat/webapps/ch1/WEB-INF/web.xml
6. tomcat/webapps/ch1/WEB-INF/src
7. tomcat/webapps/ch1/WEB-INF/classes
8. tomcat/webapps/ch1/WEB-INF/lib


Thank you again.

Anurag
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Anurag Patil wrote:6. tomcat/webapps/ch1/WEB-INF/src

Bauke has already mentioned to keep source(src) directory out of WEB-INF.
It can be, webapps/ch1/src.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
No, I didn't mean that. Ryan said less or more that WEB-INF/src must be included, which is untrue.
Anurag Patil
Greenhorn

Joined: Mar 10, 2009
Posts: 9
Thank you Bauke and Vishal. It means, it has to be(?)

1. tomcat
2. tomcat/webapps
3. tomcat/webapps/ch1
4. tomcat/webapps/ch1/WEB-INF
5. tomcat/webapps/ch1/WEB-INF/web.xml
6. tomcat/webapps/ch1/WEB-INF/classes
7. tomcat/webapps/ch1/WEB-INF/lib

nothing else, no directory in between 'webapps' and 'ch1'(?).

Thank you.

Anurag
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Deployment Environment
 
Similar Threads
Error 404 while requesting for a page
Cannot get Servlets Demystified Problem
1st servlet example
404 Error
Servlet 404 error