• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deployment Environment

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 1: move the servlet into a package other than the default.
 
Anurag Patil
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Thank you. I will try that.
 
Anurag Patil
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I didn't mean that. Ryan said less or more that WEB-INF/src must be included, which is untrue.
 
Anurag Patil
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic