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 Web Component Certification (SCWCD/OCPJWCD) and the fly likes 404 Error 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "404 Error" Watch "404 Error" New topic
Author

404 Error

pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
This is the first example from hfs i am coding....am new to tomcat ...used WSAD all along..soo
someone plz guide me

created a servlet named Ch1Servlet as below:
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><h1 align=center>HF Chapter1</h1>"+
"<br>+today+</body></html>");
}
}

created an xml file name web.xml as below:
<web-app xmls="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee"
web-app_2_4.xsd"
version="2.4">
<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>

created the following directory tree under Tomcat directory
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1\WEB-INF\classes
| |
| |
web.xml Ch1Servlet.class
and another directory tree
f:\project1
|
|
src classes etc
| |
| |
Ch1Servlet web.xml
.java

when i type http://localhost:8080/ch1/Serv1 in browser i get the error message as:

HTTP Status 404 - /ch1/Serv1

--------------------------------------------------------------------------------

type Status report

message /ch1/Serv1

description The requested resource (/ch1/Serv1) is not available.


--------------------------------------------------------------------------------
Apache Tomcat/4.1.30

can any1 plz explain the silly mistake i did here....


Thanks,<br />Pallavi
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Pallavi,

I'd think you'd need to set the content type to the response, prior to getting a PrintWriter.



Also, is your deployment descriptor (DD) directly under the WEB-INF folder? Or you'd it inside the WEB-INF/classes folder instead. It should be directly under the WEB-INF folder.

Next, is your servlet under any package? You need to specify the fully qualified class name for the servlet under the <servlet-class> element.

HTH.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
web.xml is under WEB-INF and Ch1Servlet.class in under classes
WEB-INF\classes | |
| |
web.xml Ch1Servlet.class


soo where did i go wrong?
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
i did not use any package and gave servlet class as
<servlet-class>C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1\WEB-INF\classes\Ch1Servlet</servlet-class>

still its same error
Nithya Venkatraman
Ranch Hand

Joined: Apr 11, 2004
Posts: 39
If Im right, you need to set the response type:
reponseObject.setContentType(String)

After this you can set your Print Writer etc.
Try this out.

Nithya


Nithya<br />SCJP(1.2), SCWCD(1.4)
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
i did not use any package and gave servlet class as
<servlet-class>C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1\WEB-INF\classes\Ch1Servlet</servlet-class>

still its same error
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
tried both
response.setContentType("String"); and response.setContentType("text/html");
its still the same........Servlet is fine....the prob must be in the
directory structure only
Phani Kumar Sripada
Greenhorn

Joined: Aug 14, 2003
Posts: 5
Hi Pallavi,

You shouldn't give the absolute path of the Class file in the <servlet-class> tag. The path should be relative to C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1\WEB-INF\classes.

In your case you need to give the class name <servlet-class>Ch1Servlet</servlet-class>.

Hope this helps

Regards,
Phani.


SCJP 1.4
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
Pallavi, were you able to make it work or you still need any help!?


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
S Subramonyan
Ranch Hand

Joined: Aug 26, 2004
Posts: 52
Pallavi,

You just need to give the servlet class name if its not packaged.

For eg: you have a class Serv1 in WEB-INF/classes, Serv2 in WEB-INF/classes/com/SCWCD

<servlet>
<servlet-name>name1</servlet-name>
<servlet-class>Serv1</servlet-class>
</servlet>


<servlet>
<servlet-name>name2</servlet-name>
<servlet-class>com.SCWCD.Serv2</servlet-class>
</servlet>

And don't forget to map the url pattern to the servlet name!!!
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
people its still not working for me
i think i need to rephrase my question again

created a servlet named Ch1Servlet and kept its class file under
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1\WEB-INF\classes

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

public class Ch1Servlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
java.util.Date today = new java.util.Date();
out.println("<html><body><h1 align=center>HF Chapter1</h1>"+
"<br>"+today+"</body></html>");
}
}


created an xml file name web.xml in
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1\WEB-INF

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee"
web-app_4_1.xsd"
version="4.1.30">
<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>


http://localhost:8080/ch1/Serv1 give the folling error

HTTP Status 404 - /ch1/Serv1

--------------------------------------------------------------------------------

type Status report

message /ch1/Serv1

description The requested resource (/ch1/Serv1) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/4.1.30


its not under any package
Arut Jothi
Greenhorn

Joined: Sep 14, 2004
Posts: 6
It should work if it is under webapps. However try this also,

Please go to your Tomcat 4.1\conf\server.xml and open it thru notepad/some text editor and search for the following :-

<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

and insert the follwing thing under that.

<Context path="/ch1"
docBase="C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1"
debug="5"
reloadable="true" >
</Context>

As per Tomcat 5.x, they suggest that putting <Context path....> should not be part of server.xml. Since you are using tomcat 4.0 and could not solve the problem, so I am suggesting the above.
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
there is the following in server.xml
<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true">
i added this like u said
<Context path="/ch1"
docBase="C:\Program Files\Apache Group\Tomcat 4.1\webapps\ch1"
debug="5"
reloadable="true" >
</Context>
still its the same error

i think i should give up on tomcat and better go back to WSAD!
Giju George
Ranch Hand

Joined: Jun 08, 2004
Posts: 333
Seems like tomcat is not loading your application. Try changing your web.xml as follows and restart tomcat

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

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

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


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCEA
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
noo that too is not working
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
Hi Pallavi,
Can you email me the servlet code and the web.xml file?
I am really interested to resolve this.
Pls email to these ids
bala.shekhar@aig.com
kbsekhar@yahoo.com
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
sure i will thanks a lot
Arut Jothi
Greenhorn

Joined: Sep 14, 2004
Posts: 6
It seems your web.xml has a problem here. just remove the double quote at the end and put slash.

this is what do you have:-
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee"

this is what should have
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/
jack wu
Greenhorn

Joined: Sep 08, 2004
Posts: 10
Whenever there is a problem occured,first palce you should go is C:\Program Files\Apache Group\Tomcat 4.1\logs\,see log file for some exception informations!
by the way the standards doGet signature is
Tobin Jackson
Greenhorn

Joined: Sep 01, 2004
Posts: 17
you probably already know this but.. changes to the web.xml will only take effect after you shutdown and restart tomcat. This is true even when reloadable="true" is set in your context.

tobin
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
seems like there is a problem with my tomcat will reinstall it
Giju George
Ranch Hand

Joined: Jun 08, 2004
Posts: 333
Originally posted by pallavi utukuri:
seems like there is a problem with my tomcat will reinstall it


Anyway, try to install in a directory without spaces.. like C:\Tomcat4.0 etc. I know this sounds silly, but I have read before that the spaces in directory can sometimes create problems, esp to classpath.
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
well i installed in D:\Tomcat4.1 now
http://localhost:8080/ch1/Serv1 is working
but when i start tomcat, the window is showing a buch of lines and disappearing in a second....does it work like that only or the window has to stay showing that tomcat server has started
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
when ever changes are made to servlet and is compiled why doesnt refreshing the web page show the new changed servlet???
Giju George
Ranch Hand

Joined: Jun 08, 2004
Posts: 333

but when i start tomcat, the window is showing a buch of lines and disappearing in a second....does it work like that only or the window has to stay showing that tomcat server has started


Normally the tomcat console will stay alive and you would be able to see all the system.out logs there. But if you have installed as a windows service, there won't be any console. And if you try to run the starup script, the window pops us and disappears, since there is another instance of tomcat running. So the best option is, to disable the tomcat windows service and use the startup/shutdown script.



when ever changes are made to servlet and is compiled why doesnt refreshing the web page show the new changed servlet???


For each change in the servlet, you need to restart tomcat to reflect the change. I'm not sure whether there's any other work-around for this .. !!!
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
some times restarting tomcat itself is not working......sometimes the new changes are only visible after rebooting the system! there must be some other way for the new changes to reflet..is there?
S Subramonyan
Ranch Hand

Joined: Aug 26, 2004
Posts: 52
I think changes made to the servlet will reflect on the client once you hit the refressh button. But before that check tomcat command prompt to see if "WebappClassLoader ..... updated ..was time1 : now: time2". After you see this in the tomcat window, you can successfully get the modified servlet in the browser. It might take a minute or two.
Tobin Jackson
Greenhorn

Joined: Sep 01, 2004
Posts: 17
For each change in the servlet, you need to restart tomcat to reflect the change. I'm not sure whether there's any other work-around for this .. !!!


For Tomcat 5, create a file named <web-app-name>.xml. In that file put this:


place this file in $TOMCAT/conf/Catalina/localhost/

For Tomcat 4: I think you can do the above for some of the later versions of tomcat 4. You can also set a Context tag in the server.xml file. try to google the specifics if need be.

hope this helps some folks..

tobin
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
Thanku that did work
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 404 Error
 
Similar Threads
Head First Servlets and JSP - My first servlet Program is not running
new to tomcat
problem in execution
Unable to run servlet on my machine
servlet 404 error