Master java skills

GenericServlet and HttpServlet

Two implementing classes of Servlet interface are GenericServlet and HttpServlet.

GenericServlet is protocol independent whereas HttpServlet is specific to http protocol.

To process any request, service() method is overridden. Then there are init() and destroy() methods also which can be used for initialization and clean up code respectively.

Methods summary of servlet

MethodSignatureDescription
initpublic void init(ServletConfig config)Used for initialization
servicepublic abstract void service(ServletRequest request, ServletResponse response)Used to service the incoming requests
destroypublic void destroy()Used for any clean up code
getServletConfigpublic ServletConfig getServletConfig()Used for getting the servlet config object
getServletInfopublic String getServletInfo()This returns information about the servlet
getServletContextpublic ServletContext getServletContext()This returns servlet context object
getInitParameterpublic String getInitParameter(String name)This returns the value against an initial parameter
getInitParameterNamespublic Enumeration getInitParameterNames()This returns all the initail parameters
getServletNamepublic String getServletName()This returns the name of the servlet
logpublic void log(String msg)Used for writing into log file
logpublic void log(String msg,Throwable t)Overloaded version

GenericServlet Example

package com.sks;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyGenericServlet extends GenericServlet {
	
	public void service(ServletRequest req,ServletResponse res) throws IOException,ServletException{  
	  
		res.setContentType("text/html");  
	  
		PrintWriter out=res.getWriter();  
		out.print("<html><body>");  
		out.print("<b>This response is coming from a generic servlet</b>");  
		out.print("</body></html>");  
	  
	}  
}  

HttpServlet

HttpServlet is more specific version of GenericServlet class. It extends GenericServlet and is specific to http protocol.

HttpServlet has methods specific to all http methods like for get request, we have doGet; similarly for post request, we have goPost method etc.

Methods summary for HttpServlet

Method summary
public void service(ServletRequest request, ServletResponse response)
protected void service(HttpServletRequest request, HttpServletResponse response) – This method receives the request and forwards it to doXXX() method according to the http request type.
protected void doGet(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
protected void doPut(HttpServletRequest request, HttpServletResponse response)
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
protected void doHead(HttpServletRequest request, HttpServletResponse response)
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
protected long getLastModified(HttpServletRequest request)

HttpServlet Example

package com.javatrainingschool;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MtHttpServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		
		res.setContentType("text/html");
		
		//We need printwriter object to write html content
		PrintWriter pw = res.getWriter();

		// writing html in the stream
		pw.println("<html><body>");
		pw.println("Welcome to my first servlet");
		pw.println("</body></html>");

		pw.close();// close the stream

	}
}

For complete example, click here.