Servlets beginner

John Hogan

I am studying a Java EE course at the moment and I am on the module with servlets.

Included in the course are simple sample servlets.

This may sound dumb but I cant get any of them to work either by themselves or in netbeans on the glassfish server. I have tried dropiing them in the web pages folder in the project and also I replaced the content of the index.jsp file with WelcomeServlet.html content. The example I will use her is the first one and the most simple called WelcomeServlet.

The function of the servlet is that when the user pressed the "get html document" button the program should retrieve the document from the .java file. However when I press the button I get this error

HTTP Status 404 - Not Found type Status report

messageNot Found

descriptionThe requested resource is not available.

GlassFish Server Open Source Edition 4.0

Here is the code in question. WelcomeServlet.html

    <?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. 17.6: WelcomeServlet.html -->

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

<body>
   <form action = "/advjhtp1/welcome1" method = "get">

      <p><label>Click the button to invoke the servlet
         <input type = "submit" value = "Get HTML Document" />
      </label></p>

   </form>
</body>
</html>

WelcomeServlet.java

// Fig. 16.5: WelcomeServlet.java
// A simple servlet to process get requests.
package com.deitel.advjhtp1.servlets;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class WelcomeServlet extends HttpServlet {   

   // process "get" requests from clients
   protected void doGet( HttpServletRequest request, 
      HttpServletResponse response )
         throws ServletException, IOException 
   {
      response.setContentType( "text/html" );
      PrintWriter out = response.getWriter();  

      // send XHTML page to client

      // start XHTML document
      out.println( "<?xml version = \"1.0\"?>" );

      out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
         "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
         "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); 

      out.println( 
         "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

      // head section of document
      out.println( "<head>" );
      out.println( "<title>A Simple Servlet Example</title>" );
      out.println( "</head>" );

      // body section of document
      out.println( "<body>" );
      out.println( "<h1>Welcome to Servlets!</h1>" );
      out.println( "</body>" );

      // end XHTML document
      out.println( "</html>" );
      out.close();  // close stream to complete the page
   }   
}

If anyone out there can get this code running please help me to do the same.

Duy Anh Pham

I tried your code. Indeed nothing wrong in the java code. Probably just incorrect action path.

Full project:

Project name: TestServlet

WelcomeServlet.html:

 <form action = "MyWelcomeServlet" method = "get">

web.xml:

  <servlet>
    <description>Welcome Servlet</description>
    <display-name>Welcome Servlet</display-name>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>com.deitel.advjhtp1.servlets.WelcomeServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/MyWelcomeServlet</url-pattern>
  </servlet-mapping>

Run it:

http://localhost:8080/TestServlet/WelcomeServlet.html

Click the button and it will run the servlet (you can try running the direct link to the servlet) which is:

http://localhost:8080/TestServlet/MyWelcomeServlet    

(I prefix "My" before the servlet url-pattern so that you do not confuse between the servlet and the html link). Normally it is bad practice to give same name to the servlet and the html/jsp file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related