3

this is my first servlet ever. here is it's code.

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 Chapter1 Servlet</h1>" +" " + "<br>" + today + "</body>" + "</html>");
    }
}

I compiled it using this command javac -classpath /usr/share/tomcat7/common/lib/servlet-api.jar -d classes src/Ch1servlet.java
I then put the .class file in the classes folder in my WEB-INF folder.

Here is my web.xml

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

Tomcat7 keeps giving me a 404 on http://127.0.0.1:8080/ch1/Serv1/ saying The requested resource (/ch1/Serv1/) is not available.

File Tree: File Tree

What am i doing wrong here?

prometheuspk
  • 3,754
  • 11
  • 43
  • 58
  • What about `http://localhost:8080/Serv1/`? Also you should use the fully qualified name in `servlet-class`, with the package (I hope your servlet is not in the default package). – Viruzzo Jan 18 '12 at 09:04
  • Please show us the structure of the ch1 directory, and tell us where you put it. Also tell us where you put this ch1 directory, and what the tomcat logs say. Oh, and I agree with Viruzzo: never use the default package. – JB Nizet Jan 18 '12 at 09:06
  • Check war file (or folder) name, it should be ch1. – e-zinc Jan 18 '12 at 09:08
  • tomcat/logs dir should contain log files. check and share the exception stacktraces – Murat Can ALPAY Jan 18 '12 at 09:11
  • @Viruzzo unfortunately i am using the deafault package. so tell me if my package is me.firstServlet then what would be the fully qualified name? – prometheuspk Jan 18 '12 at 09:15
  • A suggestion: start by using an IDE (like Eclipse) that is configured for Java EE projects, and let him generate the war for you; you can then look at the output structure (supposedly fully working) and start editing from there by learning what something does one piece at a time. – Viruzzo Jan 18 '12 at 09:25
  • that's what i am trying to avoid. I don't want to become IDE dependent during my learning period – prometheuspk Jan 18 '12 at 09:41

2 Answers2

6

You should put servlet classes in a package. Whether packageless servlets works depend on the specific combination of an older Tomcat and JVM version. If you see this example in a book/tutorial, then it is surely far outdated.

package com.example;

// ...

public class Ch1Servlet extends HttpServlet {
    // ...
}

You should have a /com/example/Ch1Servlet.java file. Compile it as follows

javac -classpath /usr/share/tomcat7/common/lib/servlet-api.jar -d classes src/com/example/Ch1servlet.java

(I however wonder what the common lib is doing there, this was typical for Tomcat 4.x/5.x, but it's not present since Tomcat 6. If you manually changed Tomcat's structure in order to follow the instructions of an outdated tutorial, undo it!)

Put the com folder with the generated class by its entirity in /WEB-INF/classes folder of your webapp. So you must have a /WEB-INF/classes/com/example/Ch1Servlet.class.

Then, edit your /WEB-INF/web.xml to specify the fully qualified name (FQN) of the servlet class in <servlet-class>:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
    version="3.0" 
>
    <servlet>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <servlet-class>com.example.Ch1Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Chapter1 Servlet</servlet-name>
        <url-pattern>/Serv1</url-pattern>
    </servlet-mapping>
</web-app>

(please note that I fixed the root declaration as well to comply Tomcat 7 supported servlet version, it would otherwise fall back to least compatibility modus)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It worked. Thank you so much. One thing i am wondering is the "I fixed the root declaration as well to comply Tomcat 7" part, which part is that? the version attribute? and can you please DETAIL your changes, since as you said my book is outdated (head first JSP 2008) – prometheuspk Jan 18 '12 at 15:15
  • 1
    The `` element. Yours was declared for Servlet 2.4 while Tomcat is a 3.0 container. Servlet 2.4 is from 2003. Head First JSP 2008 is just a reprint. You can by the way find nice tutorials on coreservlets.com. Start at our servlets wiki page http://stackoverflow.com/tags/servlets/info. Tutorial links are available at the bottom. – BalusC Jan 18 '12 at 15:15
  • just one more question. I want a name for my 127.0.0.1:8080 address. i guess it's called an alias. can you tell me how that's done or point me to a tut? – prometheuspk Jan 18 '12 at 15:22
  • It's called a hostname. You can edit it in `/etc/hosts` file. Just a single line `127.0.0.1 example.com` is sufficient to let your machine treat `http://example.com` as `http://127.0.0.1`. The port can't be hidden this way. You just have to reconfigure the port from 8080 (the default web development port) to 80 (the default HTTP port) in Tomcat's `/conf/server.xml`. Browsers implicitly use port 80 for HTTP requests when unspecified. If you stucks, ask a new question. This is offtopic. – BalusC Jan 18 '12 at 15:30
0

You should config your ch1 context, in server.xml ,like this:

<Context docBase="Your_web_apps_directory" path="/ch1" reloadable="false"/>
xuanyuanzhiyuan
  • 3,811
  • 1
  • 14
  • 6