0

Okay, I am a new learner and I am using as my first guide book. When I code like the book's example, I got in trouble with the beginning.

Here is the Java code:

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>");
    }
}

Here is the web.xml:

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

This book let me "javac" the 'Ch1Servlet.java' to 'Ch1Servlet.class'. Then it teached me to create document in the 'tomcat'. Here is the path.

apache-tomcat-8.0.33\webapps\ch1\WEB_INF\classes

It lets me put 'web.xml' in the 'WEB_INF' document and put 'Ch1Servlet.class' in the 'classes' document. Then start the tomcat, I can visit "http:127.0.0.1:8080" in the browser but I cannot visit "http://localhost:8080/ch1/Serv1".

I use apache 8.0.33 and I do not know what can I do with it. The book is success in the example but I failed.

My English is very poor and I am a foreigner. I know many advanced people here and could you help me? I cannot solve the problem.

Nitish Kumar Diwakar
  • 663
  • 4
  • 14
  • 25
  • Head First Servlets is from August 2004. A lot of things have changed since. According to the current industry standards, Head First Servlets contains way too much bad practices (no package declaration, writing HTML code in a Java class instead of a JSP file, writing Java code in a JSP file instead of a Java class, etc). Please move forward and look for a book targeting the currently available Tomcat / Servlet versions. Servlet classes without a `package` used to work only in ancient Tomcat+JRE versions. Check the above links and https://stackoverflow.com/tags/servlets/info to get started. – BalusC Aug 05 '17 at 10:33
  • thanks, i will change my book. – StartJava2017 Aug 06 '17 at 05:10

0 Answers0