I'm trying to step-up a simple servlet that processes a get action from a form.

When I click on the submit button I get the message.

HTTP Status 404 - /jhtp6/welcome1

type Status report

message /jhtp6/welcome1

description The requested resource (/jhtp6/welcome1) is not available.

Apache Tomcat/5.0.25

I also included an image of my file structure.

Here's the java code

3  import javax.servlet.ServletException;        
 4  import javax.servlet.http.HttpServlet;        
 5  import javax.servlet.http.HttpServletRequest; 
 6  import javax.servlet.http.HttpServletResponse;
 7  import java.io.IOException;
 8  import java.io.PrintWriter;
 9
10  public class WelcomeServlet extends HttpServlet
11  {
12     // process "get" requests from clients           
13     protected void doGet( HttpServletRequest request,
14        HttpServletResponse response )                
15           throws ServletException, IOException       
16     {
17        response.setContentType( "text/html" );
18        PrintWriter out = response.getWriter();
19
20        // send XHTML page to client
21
22        // start XHTML document                    
23        out.println( "<?xml version = \"1.0\"?>" );
24
25        out.printf( "%s%s%s" , "<!DOCTYPE html PUBLIC",
26           " \"-//W3C//DTD XHTML 1.0 Strict//EN\"",
27           " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" );
28
29        out.println( "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );
30
31        // head section of document
32        out.println( "<head>" );
33        out.println( "<title>A Simple Servlet Example</title>" );
34        out.println( "</head>" );
35
36        // body section of document
37        out.println( "<body>" );
38        out.println( "<h1>Welcome to Servlets!</h1>" );
39        out.println( "</body>" );
40
41        // end XHTML document
42        out.println( "</html>" );
43        out.close();  // close stream to complete the page
44     } // end method doGet
45  } // end class WelcomeServlet

Here's the xml Code

<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">                                       
 
      <!-- General description of your Web application -->
      <display-name>                                      
         Java How to Program JSP                          
        and Servlet Chapter Examples                     
     </display-name>                                     

     <description>                               
        This is the Web application in which we  
        demonstrate our JSP and Servlet examples.
     </description>                              

     <!-- Servlet definitions -->                            
     <servlet>                                               
        <servlet-name>welcome1</servlet-name>                
                                                             
        <description>                                        
           A simple servlet that handles an HTTP get request.
        </description>                                       
                                                             
        <servlet-class>                                      
           WelcomeServlet                                    
        </servlet-class>                                     
     </servlet>                                              

     <!-- Servlet mappings -->               
     <servlet-mapping>                       
        <servlet-name>welcome1</servlet-name>
        <url-pattern>/welcome1</url-pattern> 
     </servlet-mapping>                      

  </web-app>

and here's the html document code

<?xml version = "1.0"?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

   <!-- Fig. 26.7: WelcomeServlet.html -->
 
   <html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>Handling an HTTP Get Request</title>
  </head>

  <body>
     <form action = "/jhtp6/welcome1" method = "get" >
        <p><label>Click the button to invoke the servlet
           <input type = "submit" value = "Get HTML Document" />
        </label></p>
     </form>
  </body>
  </html>

Why do I get this message instead of the servlet response and how do I fix it.

Your servlet's .class file should be in WEB-INF/classes/ is that the case ?

Also once you move your servlets .class file there, you will need to either restart tomcat entirely or you will need to reload you application, by accessing the tomcat manager at http://localhost:8080/manager/html, for the URL I am assuming your tomcat is running on the same machine as you are working on and is configured to listen on port 8080 for requests

hi,
error no: File Not Found Error.

So, please check the file name and the class name are same, to check correct path to save the file.
save the correct location and try again.....

<?xml version = "1.0"?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

   <!-- Fig. 26.7: WelcomeServlet.html -->
 
   <html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>Handling an HTTP Get Request</title>
  </head>

  <body>
     <form action = "welcome1" method = "get" >
        <p><label>Click the button to invoke the servlet
           <input type = "submit" value = "Get HTML Document" />
        </label></p>
     </form>
  </body>
  </html>

Change the html given by u and put the above. The <form action="/jhtp6/welcome1" method="get"> has been changed to <form action="welcome1" method="get">

Hope it works

Salamo Alaikum Knight

I had the same error as yours; mine was solved that way

instead of <form action = "/jhtp6/welcome1" method = "get" >
it has to be <form method = "get" action = welcome1 >

Hope it works
Please give me feed back

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.