wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet is not running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet is not running" Watch "Servlet is not running" New topic

Servlet is not running

V K Gupta
Ranch Hand

Joined: Aug 07, 2008
Posts: 55

I am trying to run Example from Head first Servlets & JSP given on page no 30 (e.g servlet which shows the time)

But as I run the example on IE, i get an error like this

HTTP Status 404 - /ch1/Serv1


type Status report

message /ch1/Serv1

description The requested resource (/ch1/Serv1) is not available.


Apache Tomcat/6.0.18

Contents of web.xml are
<?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-mapping>
<servlet-name>Chapter1 Servlet</servlet-name>

contetns of Ch1Servlet.java are
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>Chapter 1</h1>" +
" + today + "</body>" + "</html>");

Please tell me where I am making an mistake
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

How did you deploy the code??

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105


I too had a same problem. If your deployment is correct and still you're getting an error, then you should change your dd simply as:

Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162

where do you paste yout classes files, i think you might have done the mistake here.

you have to paste the class files inside WEB-INF/classes folder in the web-application directory.

Thanks & Regards,
Bennet Xavier.
Abdul Rahman
Ranch Hand

Joined: Jul 04, 2008
Posts: 63
even i've the same problem.

i'm trying to run a servlet example. i'm getting a blank page after clicking submit button. Please help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<h3>Please enter your userid and password to see your account statement:</

<form action="LoginServlet" method="POST">
Userid : <input type="text" name="userid">

Password : <input type="password" name="password">

<input type="submit" value="Show Statement">

public class LoginServlet extends HttpServlet {

Hashtable users = new Hashtable();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String userid=request.getParameter("userid");
String pword =request.getParameter("password");
if(userid !=null && pword !=null && pword.equals(users.get(userid))){
request.setAttribute("userId", userid);
ServletContext sc= getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/AccountServlet");
rd.forward(request, response);
RequestDispatcher rd = request.getRequestDispatcher("/login.html");
rd.forward(request, response);
public void init() throws ServletException{
users.put("user1", "aaa");
users.put("user2", "bbb");
users.put("user3", "ccc");
<description>wcd test project</description>
<display-name>wcd test project</display-name>
<description>wcd test project</description>
<display-name>wcd test project</display-name>

public class AccountServlet extends HttpServlet {

Hashtable data = new Hashtable();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String userId = (String)request.getAttribute("userid");
String[] records = (String[])data.get(userId);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.println(", using the POST method");
out.println("<h3>Account Status for "+userId+" at the start of prev 3 months");
for(int i=0;i<records.length;i++){
out.println(" </BODY>");
public void init() throws ServletException {
data.put("ann", new String[]{ "01/01/2002 : 1000.00",
"01/02/2002 : 1300.00", "01/03/2002 : 900.00"} );
data.put("john", new String[]{ "01/01/2002 : 4500.00",
"01/02/2002 : 2100.00", "01/03/2002 : 2600.00"} );
data.put("mark", new String[]{ "01/01/2002 : 7800.00",
"01/02/2002 : 5200.00", "01/03/2002 : 1900.00"} );


please advise where i'm missing something...

SCJP5, SCWCD5, [scwcd wall of fame]
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi Abdul Rahman,

this problem is because the you are using userid in AccountServlet.java and userId in LoginServlet.java

you are using different I in userid attribute.

correct this.

because of this its getting null value, which in turn you are using it to get the values from Hashtable.

and change the key values in the Hashtable in AccountServlet.java

otherwise it again shows you null pointer Exception.

Bennet Xavier. X
I agree. Here's the link: http://aspose.com/file-tools
subject: Servlet is not running
Similar Threads
Not able to run servlet-HTTP status404
new to tomcat
problem in execution
HTTP Status 404 The requested resource () is not available
404 Error