• 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

HTTP Status 404 - /Ch1/Serv1

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class is

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\'s Chap1 Servlet</h1>"+"
"+today+"</body>"+"</html>");
}
}


and the web.xml is

<?xml version="1.0" encoding="ISO-8859-1"?>
<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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>Chapter1Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Chapter1Servlet</servlet-name>
<url-pattern>Ch1/Serv1</url-pattern>
</servlet-mapping>
</web-app>


The web xml is in the WEB-INFO folder.I am facing hard time running this simple servlet program..and need help..
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Geet!

1. Please use [code] tag to put your code - it will definitely increase the readability of your example.
2. I would suggest you to use your own packages instead of the default one, as this might end in hard-to-find bugs,
3. What URL do you invoke?
4.

The web xml is in the WEB-INFO folder.

The WEB-INFO/ directory doesn't have any special meaning. However, the WEB-INF/ does.
5. What is your webapp directories structure?

Cheers!
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check your container log files for any indication of an error.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Pedro

The URL invoked is http://localhost:8080/Ch1/Serv1..and sorry that was a typo..the web.xml is in WEB_INF
Do you mean that i should put that i should put this web.xml file in a package??
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tom

I checked the log file too.But i cudnt get through what exactly the problem was.Please help
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geet kaur wrote:(...) and sorry that was a typo..the web.xml is in WEB_INF



So it's finally WEB_INF/ or WEB-INF/ ? ;-)


geet kaur wrote:Do you mean that i should put that i should put this web.xml file in a package??



No, I mean that you should put your Ch1Servlet class into the package - did you do it ever before?


Oh, and please post your web app directory structure (where did you put your *.class files, where is WEB-INF, where is web.xml, where is the whole application directory located in your servlet container, etc.)


EDIT: Oh, and I did notice that you missed the "/" at the beginning of your url-pattern.
EDIT2: Yeah, and you didn't specify the servlet context when typing your URL, so the place (like ROOT directory in Tomcat servlet container) where the web-app is deployed in your servlet container is also important in this case.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Pedro
A typo again.. ..its WEB-INF
The Directory structure is: D:\Project1\Src\Ch1Servlet.java,D:Project1\Classes\Ch1Servlet.class,Project1\Etc\Web.xml

and for the webapp the structure is C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Ch1\WEB-INF\Classes\Ch1Servlet.class
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Ch1\WEB-INF\web.xml



I have rectified the "/" thing.But still its not showing up
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I see that you still don't want to put your class into your package :-)

Despite this, you are using an inappropriate URL to access your servlet. You have a application context (servlet context) binded to /Ch1 (this is the directory name in webapps directory of your Tomcat servlet container). Your URL mapping is /Ch1/Serv1.
The URL you need to invoke in order to access the Chapter1Servlet is a concatenation of application context (servlet context) and URL mapping and therefore it's equal to:

http://localhost:8080/Ch1/Ch1/Serv1
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am not comfortable with the package thing,but still i will put it in a package now,i need to put the Ch1servlet.java(which i hav alloacted in
the D dir??).and i tried calling it through the url you just mentioned,but its still showing up the same error
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some resources about packages, straight from uncle Google:
http://en.wikipedia.org/wiki/Java_package
http://www.jarticles.com/package/package_eng.html

In my opinion, you really should feel comfortable with this topic, especially if you want to create web applications (or basically ANY Java application).

Also be careful when naming your directories. I am not sure if the directory names are case-sensitive, but it's best to keep the lowercase notation (I'm talking about your 'Classes' directory). I don't think it should be the problem, as you should see ClassNotFound exception or some other indicator that the mapping was matched fine, but the servlet code couldn't be found.

By the way - do you constantly check the log files for any abnormal activity?

Cheers!
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh no,i am now getting the error 500 classNotFoundException ..and its only been a day that i have started advanced java ..so i am now a bit
able to understand what the log files show..what exactly caused this error??
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try changing the name of your 'Classes' directory to 'classes':

geet kaur wrote:and for the webapp the structure is C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Ch1\WEB-INF\Classes\Ch1Servlet.class




By the way - what book are you reading? Did you write any Java applications before?
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading head first servlets and jsp by k&b.yes i appeared for scjp long ago in 2008 ,wanted to go for scwcd but then started working on csharp.
and now i want to switch thats why have started learning it.
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so you're reading the right book definitely

Did the name change of classes directory work?
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No still its not working..
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

No still its not working..


Don't worry, many people are struggling in the beginning. Just try to create a .war file of your web-application by hand.

Have a look at the following instruction and pay attention to the directory structures.

Regards,
Frits
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finalllllyyyyyyy......Its done...Directory structure was the problem...thanks a tonn guys,you guys rock
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad it worked! :-)

And for all the followers who will struggle with the same problem - what exactly was messed up?

 
Ranch Hand
Posts: 153
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this topic is old, but I would like to add something very, very important.

If you have read other people's posts on this specific topic (404 not found, first servlet example) and you still can't get it to work,
well.. here is the solution:

Remove the / at the end of the URL. (xxxx = tomcat port)
It should then be: http://127.0.0.1:xxxx/ch1/Serv1

I have been pulling out my hairs on this specific problem for two hours and the solution was to remove the last /
It's just.. crazy.

In conclusion, the most common reasons why you are getting a 404 on this example are:

- you need to change ISO-8851-1 to ISO-8859-1 in your web.xml file that is present under the ch1/WEB-INF/ directory under your tomcat/web-apps directory.
- you need to remove the last forward slash from your URL, because you wrote the example just like in the book, without the / in the <url-mapping>. Remember? It was /Serv1. There is no extra / there.
More fundamentally, a lot of webservers (such as Apache) allow http://my.domain.com to be written as http://my.domain.com/ (first it gives a 301 'moved', but then later fixes this error implicitly by itself).

I'm actually glad I am following the book, doing everything hard coded in notepad.
This is how I catch these hard to find problems.

Sincerely,

--
Ryan
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan McClain wrote:I know this topic is old, but I would like to add something very, very important.

If you have read other people's posts on this specific topic (404 not found, first servlet example) and you still can't get it to work,
well.. here is the solution:

Remove the / at the end of the URL. (xxxx = tomcat port)
It should then be: http://127.0.0.1:xxxx/ch1/Serv1

I have been pulling out my hairs on this specific problem for two hours and the solution was to remove the last /
It's just.. crazy.

In conclusion, the most common reasons why you are getting a 404 on this example are:

- you need to change ISO-8851-1 to ISO-8859-1 in your web.xml file that is present under the ch1/WEB-INF/ directory under your tomcat/web-apps directory.
- you need to remove the last forward slash from your URL, because you wrote the example just like in the book, without the / in the <url-mapping>. Remember? It was /Serv1. There is no extra / there.
More fundamentally, a lot of webservers (such as Apache) allow http://my.domain.com to be written as http://my.domain.com/ (first it gives a 301 'moved', but then later fixes this error implicitly by itself).

I'm actually glad I am following the book, doing everything hard coded in notepad.
This is how I catch these hard to find problems.

Sincerely,

--
Ryan



Thanks Ryan, changing ISO encoding solves my problem.
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic