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:

What am i doing wrong here?
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.
You should have a
/com/example/Ch1Servlet.javafile. Compile it as follows(I however wonder what the
commonlib 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
comfolder with the generated class by its entirity in/WEB-INF/classesfolder of your webapp. So you must have a/WEB-INF/classes/com/example/Ch1Servlet.class.Then, edit your
/WEB-INF/web.xmlto specify the fully qualified name (FQN) of the servlet class in<servlet-class>:(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)