What is Servlet?
Java Servlet
is programs that run on a Web or Application server and act as a middle layer
between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
Types of Servlet
Generic Servlet
The Generic Servlet is an abstract class that is extended by
HttpServlet to provide HTTP protocol-specific methods.
The Generic Servlet does not include protocol-specific methods for
handling request parameters, cookies, sessions and setting response headers.
Generic Servlet is not specific to any protocol.
HTTP Servlet
An abstract class that simplifies writing HTTP servlet. It extends
the GenericServlet base class and provides an framework for handling the HTTP
protocol.
The HttpServlet subclass passes generic service method requests to
the relevant doGet () or doPost () method.
HttpServlet only supports HTTP and HTTPS protocol.
HelloWorld Program
import java.io.*;
import java.text.*;
import java.util.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
public class
HelloWorldExample extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse
response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("<h1>HelloWorld</h1>");
out.println("</body>");
out.println("</html>");
}
}
Servlet Packages
HelloWorld Program of Servlet
1. import
package
import
java.io.*;
This package is
for input /output operation.
import
javax.servlet.*;
import
javax.servlet.http.*;
This two
package mainly responsible for Servlets
implementation.
Servlet life cycle
A servlet life cycle
can be defined as the entire process from its creation till the destruction.
The following are the paths followed by a servlet.
The servlet is initialized by calling the init () method.
The servlet calls service() method to process a client's request. The servlet is terminated by calling the destroy() method. Finally, servlet is garbage collected by the garbage collector of the JVM.
The servlet is initialized by calling the init () method.
The servlet calls service() method to process a client's request. The servlet is terminated by calling the destroy() method. Finally, servlet is garbage collected by the garbage collector of the JVM.
What is the
difference between difference between doGet() and doPost()?
doGet () and doPost() are HTTP requests handled by servlet
classes.
In doGet (), the parameters are appended to the URL
and sent along with header information. This does not happen in case of
doPost(). In doPost(), the parameters are sent separately. Since most of the web servers support only a limited
amount of information to be attached to the headers, the size of this header
should not exceed 1024 bytes. doPost() does not have this constraint. Usually programmers find it difficult to choose between doGet()
and doPost(). doGet() shall be used when small amount of data and
insensitive data like a query has to be sent as a request. doPost() shall be used when comparatively large
amount of sensitive data has to be sent. Examples are sending data after
filling up a form or sending login id and password.