The GenericServlet class
implements the Servlet and ServletConfig
interfaces. Since service () method is declared as an abstract method
in GenericServlet class, it is an abstract class. The class extending this
class must implement the service () method. It is used to create servlets which
are protocol independent.
To understand the creation of servlets using the GenericServlet class, lets us consider a simple program given here.
For this, two files will be required; one .java file in which servlet is defined
and another. html file containing code for web page.
Pros of using Generic Servlet:1. Generic Servlet is easier to write
2. Has simple lifecycle methods
3. To write Generic Servlet you just need to extend javax.servlet.GenericServlet and override the service() method
2. Has simple lifecycle methods
3. To write Generic Servlet you just need to extend javax.servlet.GenericServlet and override the service() method
Cons of using Generic Servlet:Working with Generic Servlet is not that easy because we don’t have convenience methods such as doGet(), doPost(), doHead() etc in Generic Servlet that we can use in Http Servlet.
In Http Servlet we need to override particular convenience method for particular request, for example if you need to get information then override doGet(), if you want to send information to server override doPost(). However in Generic Servlet we only override service() method for each type of request which is cumbersome.
In Http Servlet we need to override particular convenience method for particular request, for example if you need to get information then override doGet(), if you want to send information to server override doPost(). However in Generic Servlet we only override service() method for each type of request which is cumbersome.
Methods of GenericServlet class
There are many methods in GenericServlet class. They are as follows:
- public void init(ServletConfig config) is used to initialize the servlet.
- public abstract void service(ServletRequest request, ServletResponse response) provides service for the incoming request. It is invoked at each time when user requests for a servlet.
- public void destroy() is invoked only once throughout the life cycle and indicates that servlet is being destroyed.
- public ServletConfig getServletConfig() returns the object of ServletConfig.
- public String getServletInfo() returns information about servlet such as writer, copyright, version etc.
- public void init() it is a convenient method for the servlet programmers, now there is no need to call super.init(config)
- public ServletContext getServletContext() returns the object of ServletContext.
- public String getInitParameter(String name) returns the parameter value for the given parameter name.
- public Enumeration getInitParameterNames() returns all the parameters defined in the web.xml file.
- public String getServletName() returns the name of the servlet object.
- public void log(String msg) writes the given message in the servlet log file.
- public void log(String msg,Throwable t) writes the explanatory message in the servlet log file and a stack trace.
Example of GenericServlet Class
This servlet program is used to print "Hello World" on client browser using GenericServlet class.
HelloWorld.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* This
servlet program is used to print "Hello World" on
*
client browser using GenericServlet class.
*
@author javawithease
*/
public class HelloWorld extends
GenericServlet {
private static final long
serialVersionUID = 1L;
//no-argument
constructor.
public HelloWorld() {
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out =
response.getWriter();
out.println("<h1>Hello
World example using" +
"
GenericServlet class.</h1>");
out.close();
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
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">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>
com.javawithease.business.HelloWorld
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
0 comments:
Post a Comment