Starting with servlets(java)

                          Starting with servlets (java)

In today's post i would like to share my views about servlets . First of all i would start with introduction with what are servlets ..?

As there are two types of web page one is the static one and other is the dynamic one as we know we can create dynamic web pages with the help of php we can create the same dynamic web page with the help of servlets so the java concepts of creating the dynamic web pages are the servlets.

Servlets are the ones we need to create a web application, it is server side language so it uses a web server to run/execute.


In java we have javax.servlet package in it we have servlet interface so we it is understood from this that if we want to create servlet we need to implement servlet interface for it . So definitely some methods might have been declared in it that we have to define in our own class when we want to make servlet by implementing interface with our class.

Methods are 

MethodDescription
public void init(ServletConfig config)initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once.
public void service(ServletRequest request,ServletResponse response)provides response for the incoming request. It is invoked at each request by the web container.
public void destroy()is invoked only once 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.

Now taking an abstract class that we have is the GenericServlet class which implements Servlet so its obvious that a class implementing servlet interface might have defined its declared methods so GenericServlet class is also provided by java so why it is an abstract class because in this class one method known as service method is declared abstract so any class having a single method as abstract is known as an abstract class.

Now if we extend GenericServlet with our class than we need not to define init() destroy() and all the methods listed in the above table except Service() method.

How to define  GenericServlet 

import javax.Servlet.*;

public class myServlet extends GenericServlet
{
// only Service method  is needed to define 

}

                                                                                                                                                  

Now  moving on httpServlet ,  httpServlet is derived class of GenericServlet so definitely one method declared as abstract in GenericServlet is defined here so Now if any class which extends httpServlet have no need to defined any methods mentioned in the above table.

For httpServlet

import javax.Servlet.*;
import javax.Servlet.http.*;

class abc extends httpServlet
{

Service method is already defined so no need to define it .

}


This was all about today's post .... !! Give your valuable feedback..!! 
























Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another