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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet is not running" Watch "Servlet is not running" New topic
Author

Servlet is not running

V K Gupta
Ranch Hand

Joined: Aug 07, 2008
Posts: 55
Hi,

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-class>Ch1Servlet</servlet-class>
</servlet>
- <servlet-mapping>
<servlet-name>Chapter1 Servlet</servlet-name>
<url-pattern>/Serv1</url-pattern>
</servlet-mapping>
</web-app>



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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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


Hi,

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

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.

Login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>SCWCD_Example_1_3</title>
</head>
<body>
<h3>Please enter your userid and password to see your account statement:</
h3>


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


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


<input type="submit" value="Show Statement">
</form>
</body>
</html>

LoginServlet.java
public class LoginServlet extends HttpServlet {

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

response.setContentType("text/html");
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);
return;
}else{
RequestDispatcher rd = request.getRequestDispatcher("/login.html");
rd.forward(request, response);
return;
}
}
public void init() throws ServletException{
users.put("user1", "aaa");
users.put("user2", "bbb");
users.put("user3", "ccc");
}
}
web.xml
<servlet>
<description>wcd test project</description>
<display-name>wcd test project</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>wcd test project</description>
<display-name>wcd test project</display-name>
<servlet-name>AccountServlet</servlet-name>
<servlet-class>com.example.servlets.AccountServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AccountServlet</servlet-name>
<url-pattern>/AccountServlet</url-pattern>
</servlet-mapping>

AccountServlet.java
public class AccountServlet extends HttpServlet {

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

response.setContentType("text/html");
String userId = (String)request.getAttribute("userid");
if(userId!=null){
String[] records = (String[])data.get(userId);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
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(records[i]+">
");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
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...



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

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