Servlet API:
To develop servlets we need SERVLET API support. We have 2 Packages for
Developing Servlet.
- javax.servlet
- javax.servlet.http
servlet:
this interface
provides life cycle methods for our servlets. The life cycle methods are
- init(ServletConfig)
- service(ServletRequest,ServletResponse)
- destroy()
in our Servlet class we override these methods. Servlet engine calls
these methods implicity.servlet interface has 2 more methods, which are non
life cycle methods
- getServletConfig():- it return ServletConfig object
- getServletInfo():- it returns a string that gives servlet information
GenericServlet:-
This is an abstract class. This class implements
servlet interface. It defined init(ServletConfig) and destroy() methods of the
servlet interface. The third life cycle method service() is not defined in the
GenericServlet class. This class also implements the ServletConfig interface.
Therefore all the methods of ServletConfig interface can be called directly on
the servlet interface itself. GenericServlet class defines one zero argument
init method. Parameterized init method defined by the GenericServlet calls this
zero argument init method. Therefore servlet developers can override only zero
argument method in their servlet classes.
HttpServlet:-
This is an abstract class. It is a sub class of GenericServlet
class. Servlet developers always defines their servlet class by extending this
class. service method of the GenericServlet is implemented in this class. in
addition to the public service method ,HttpServlet class has defined the
following methods.
- protected void service(HttpServletRequest, HttpServletResponse)
- protected void doGet(HttpServletRequest, HttpServletResponse)
- protected void doPost(HttpServletRequest, HttpServletResponse)
servlet engine calls only public service method,
within the public service method protected service method is called. Within the
protected service method of the HttpServlet class , either doGet or doPost
method is called depending upon thetype of request (GET or Post) coming from the client. In our servlet
class we always overrides either doGet() or doPost() .we never overrides the
public service() or protected service().
ServletConfig:-
This is an
interface in java.servlet package.
Servlet engine writes a sub class of
ServletConfig interface and creates its instance. But we call it as
ServletConfig object only.
Methods:
- String getInitParameter(String): this method is used to get init param value by supplying param name as input.
- ServletContext getServletContext() :-when we will call this method it returns the ServletContext object.
ServletContext:-
This
is an interface in java.servlet package.
Servlet engine writes a sub class of
ServletContext interface and creates its instance. But we call it as
ServletContext object only. In the servlet programming this object is very
widely used. For a servlet this object is directly available. We call a method
in servlet to get the ServletContext object.
ServletContext sc=getServletContext();
In the above statement we are calling getServletContext() on the
servlet interface. But internally it is called on the ServletConfig object.
Methods:
- getInitParameter() :- it takes context param name and returns the corresponding value.
- setAttribute() : -stores a data item in context scope with name ,value pair.
- getAttribute() :-takes the attribute name and returns the value.
- removeAttribute() :-delete the data item from the context scope.
- getRequestDispatcher() :-this method returns the RequestDispacher object.
- getServerInfo() :-it returns the web container information as a string .
- log() :-used for store the information into web container log files.
HttpServletRequest:
This is an interface in javax.servlet.http package. it
as sub interface of ServletRequest interface. Servlet engine writes a sub class
of HttpServletRequest interface and creates its instance. But we call it as
HttpServletRequest object only.
Methods:
- getParameter() :- it takes html control name and returns the user input
- setAttribute() : -stores a data item in request scope with name ,value pair.
- getAttribute() :-takes the attribute name and returns the value.
- removeAttribute() :-delete the data item from the request scope.
- getRequestDispatcher() :-this method returns the RequestDispacher object.
- getCookies() :-it returns an array of Cookies coming from the browser.
- getSession(() :-returns the HttpSession Object which is unique for the client.
- getQueryString() :-Returns the query string that is contained in the request URL after the path.
- getHeaderNames() :-Returns an enumeration of all the header names this request contains.
- getHeader() :-it takes the header name and returns the header value.
HttpServletResponse:
This is an interface in javax.servlet.http package. it
as sub interface of ServletResponse interface. Servlet engine writes a sub
class of HttpServletResponse interface and creates its instance. But we call it
as HttpServletResponse object only.
Methods:
- getWriter() :-it returns the PrinterWriter object.
- setContentType() :-this method set the MIME type for the response.
- getOutputStream() :-returns the OutputStream object.
- addCookie() :-it addsa cookie to the response header.
- encodeURL() :- this method is used to URL rewriting .
- sendRedirect() :-sends a temporary redirect response to the client.
- sendError() :-sends an error response to the client using error status.
- setHeader() :- sets a response header with the given name and value.
- addHeader() :-adds a response header with the given name and value.
RequestDispacher:-
It is an
interface. Servlet container writers a sub class for this interface. Servlet
engine produces this sub class object when we call getRequestDispacher() on the
request or context object. But we say that object as RequestDispacher object
only.
Methods:
- forward() :- it forwards the control from the servlet to another web resource.
- Include() :-it includes the response of other web resource into the current servlet response.
Directory Structure:
Root Directory
WEB-INF
classes
lib
src
web.xml
Resource
files
Root directory name should be anything usually it is name of the
project/application
classes directory used to keep the java class file(.class files)
lib directory contains the jar file(.jar files)
src or source directory for java files(.java)
web.xml file is call deployment descriptor file
the above files and directories are under “WEB-INF” directory
resource contains the image and html or jsp file …….etc.
Example Project