4
votes

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.


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

2 Answers

0
votes

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.

2
votes

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.