views:

46

answers:

3

I am making a small website as my first project. I have finalized to use Java Servlets and JSP for my Server-side scripting. I am learning it from O'Reilly's HeadFirst Servlets and JSP. I decided to use Apache-Tomcat as my web server and container. I downloaded it. I even have jdk 1.6 update 21. I unzipped apache in C: It is running successfully as i get the default Tomcat home page when I type http://localhost:8080 in my browser.

In chapter 3 there is a small project called beer. So I tried to make it. I created a directory called beer under webapps C:\apache-tomcat-7.0.0\webapps\beer. Inside it to just check the basics I created a html file called form.html. But when I try to access it via the browser http://localhost:8080/beer/form.html I get the following error:

HTTP Status 404 - /beer/form.html

type Status report

message /beer/form.html

description The requested resource (/beer/form.html) is not available.

Apache Tomcat/7.0.0

I have even tried using tomcat 6, but to no use.

A: 

The problem almost certainly is within your servlet declaration.

There is a file called web.xml within the WEB-INF folder of your webapp (/webapps/beer/WEB-INF). See if you can find it and post its content please.

This file declares how servlets will be mapped to request urls. Thus if there is a servlet mapped to the url extension /*:

   <servlet-mapping>
      <servlet-name>ServletName</servlet-name>
      <url-pattern>/*</url-pattern>
   </servlet-mapping>

you will not be able to access files directly (i.e. /beer/form.html won't retrieve form.html) since the servlet ServletName will intercept any request on a url that starts with http://localhost:8080/beer/.


Btw, in case you wondered: the status code HTTP 404 means that the file the url points to was not found.

See http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error for more info on HTTP status codes.

FK82
Sorry, but this answer is utterly wrong. A new folder doesn't require another servlet at all.
BalusC
I didn't say that. Merely stating a possibility.
FK82
It's still wrong. For one thing, his "application" isn't using any servlets. For another, the `url-pattern` doesn't come into play until *after* the application name, whose mapping is not by default controlled by `web.xml`.
Carl Smotricz
I was checking for that possibility in my answer. I don't understand your problem. I changed the `url-pattern` to `/*` if that is what your fuss is about.
FK82
The OP is following a tutorial from O'Reilly. Who said it was empty?
FK82
Even then, this should not have produced a 404, but rather a different or blank page or a 5nn error. Unless the servlet is written on a smart way that it did `resposne.sendError(404);`, but I don't think the OP already got that far.
BalusC
Honestly BalusC, I got that error a thousand times when I made the same mistake to request a file via a URL which was getting intercepted by a servlet. It produces a 404 status. That's why I posted this. I don't see your point.
FK82
A: 

Thank you for replying... Actually my doubt is..that here i am trying to access a .html file which is not a servlet so does it even require a Deployment description????

well, there is only 1 servlet i planned to make (initially)....the one which is mentioned in the form (action= "....") in the html file i made.... so i thought of checking the basic html file before i deployed a servlet....btw...if i have to include the html file also in the DD, then what should i write in place of ServletName in the xml code u gave? here are the contents of my web.xml (i modified it as u said...but dunno what to write in the Servlet Name)

<?xml version="1.0" encoding="utf-8"?>
 <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_2_4.xsd" version="2.4">
    <servlet>
        <servlet-name>html page</servlet-name>
        <servlet-class>form</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>html page</servlet-name>
      <url-pattern>/beer/form.html</url-pattern>
   </servlet-mapping>


    <servlet>
        <servlet-name>Ch3 Beer</servlet-name>
        <servlet-class>com.example.web.BeerSelect</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Ch3 Beer</servlet-name>
        <url-pattern>/SelectBeer.do</url-pattern>
    </servlet-mapping>
</web-app>
Shahensha
Hi, this isn't an answer. This is more a comment on other answer and a update on the question combinied. You should use `add comment` link to add a comment on other's answer and you should use `edit` link below question to add updates to question relevant to everyone. Once done that, you should click the `delete` link to delete this "answer" :) See also http://stackoverflow.com/faq
BalusC
@BalusCI tried doing that first....but fk82 wanted me to post the contents of web.xml and all that wasn't fitting in the comments...i was overrunning the char limit...so i did this...is there sum other way?...i'll delete this then.
Shahensha
@Shahensha: If there's an `edit` link under your post, you can edit your post to your heart's content, and of course paste in any code. I'm not sure if there's some minimum point level you need before you can edit your own posts. Anybody know?
Carl Smotricz
Anyway, I see a problem in the above web.xml . "form.html" is not a servlet. The whole top 2 elements `servlet` and `servlet-mapping` are wrong and should be removed. If `form.html` is in your webapps/beer folder, it should be accessible by default just as you tried before.
Carl Smotricz
To answer your other question: Tomcat is a Web App container; as such it likes to deal with *applications*. I have on occasion built an "application" consisting only of a `WEB-INF/web.xml` and one or more HTML pages, but that is the minimum required. The `web.xml` doesn't need to contain much; I usually include a `display-name` and a `welcome-page`. The welcome page would help you too, as you could then access your `...beer/form.html` simply with `...beer` if you so wanted.
Carl Smotricz
@ Shahensha: A simple file won't need a deployment descriptor as it is not a servlet. I did not say that either :) Sorry if my answer mislead you. If there is no servlet mapped to the url, I'd suspect a typo as well.
FK82
A: 

It should work just fine. Probably a typo in URL or filename. It's case sensitive as well. /Beer is not the same as /beer. Doublecheck the URL. Also, don't you have multiple instances of Tomcat running or extracted from the downloaded zip? You might have placed the new page in the wrong one and/or be running the wrong one. Try shutting down the Tomcat instance you think you're running and reload the homepage in the webbrowser. If the homepage doesn't disappear, then it's a different one.

BalusC
@BalusCI checked my url many times...also i shut both the servers 6.0 and 7.0 and then strtd only 1...still the problem persists...even the home page loads properly..i am perplexed
Shahensha
@balus Ci got the problem...u were right....i did a very shitty mistake in naming my file...thanks a lot...ur post made me check it again and again...and i got the problem
Shahensha
You're welcome. You should then mark this answer accepted :)
BalusC