Example-Simple Http Servlet package itso.servjsp.servletapi; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; pu...
Example-Simple Http Servlet
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleHttpServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleHttpServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
}
As the title indicates, SimpleHttpServlet is a very simple HTTP servlet that accepts a request and writes a response. Let’s break out the components of this servlet so we can discuss them individually.
Basic servlet structure
Basic servlet structure
Example given below shows that we have defined this servlet to be part of an itso.servjsp.servletapi Java package.
package itso.servjsp.servletapi;
Code given below shows the import statements used to give us access to other Java packages. The import of java.io is so that we have access to some standard IO classes. More importantly, the javax.servlet.* and javax.servlet.http.* import statements give us access to the Java Servlet API set of classes and interfaces
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.http.*;
Next section is the heart of this servlet, the implementation of the service method for the handling of the request and response objects of the servlet.
protected void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
What the service method doesthrows ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
Let’s examine this service method in more detail. Notice that the method accepts two parameters, HttpServletRequest and HttpServletResponse. The request object contains information about and from the client. In this example, we don’t do anything with the request.
This method is declared Abstract in the basic GenericServlet class, and so subclasses, such as HttpServlet, must override it. In our subclass of HttpServlet, when using this method, we must implement this method according to the signature defined in HttpServlet, namely, that it accepts HttpServletRequest and HttpServletResponse arguments.
We do some handling of the response object, which is responsible for sending our response back to the client. Our response here is a formatted HTML page, so we first set the response content type to text/html by coding res.setContentType("text/html"). Next, we request a PrintWriter object to write text to the response by coding
PrintWriter out = res.getWriter().
We could also have used a ServletOutputStream object to write out our response, but getWriter gives us more flexibility with Internationalization. In either case, the content type of the response must be set before references to these objects can be made.
The remaining out.println statements write our HTML to the PrintWriter, which is sent back to the client as our response. It is pretty simple HTML, so we do not display it here. We use out.close more for completeness, because the Web application server automatically closes the PrintWriter when the service method exits.