• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet is not running

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you deploy the code??
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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:



 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic