Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

02/10/2016 Categories: Servlets. No Comments on Servlet API

Servlet API is a set of classes and interfaces that specify a contract between a servlet class and a servlet container. Some examples of servers that provide servlet containers are: Apache Tomcat, Oracle’s WebLogic, IBM’s Websphere, JBoss etc. All the API classes and interfaces are grouped into following packages:

  • javax.servlet
  • javax.servlet.http

 

javax.servlet Package

 

Provides a set of classes and interfaces that describe and defined the contract between the servlet class and the runtime environment provided by the servlet container. Following are the interfaces available in this package:

javax-servlet-interfaces

 

Following are the classes available in this package:

javax-servlet-classes

 

Following are the exception classes available in this package:

javax-servlet-exceptions

 

javax.servlet.http Package

 

Provides a set of classes and interfaces that describe and define the contract between the servlet class running under the HTTP protocol and the runtime environment provided by the servlet container. HttpServlet class implements the Servlet interface.

 

Following are the interfaces available in this package:

javax-servlet-http-interfaces

 

Following are the classes available in this package:

javax-servlet-http-classes

 

javax.servlet.Servlet Interface

 

The Servlet interface provides a standard abstraction for the servlet container to understand the servlet object created by the user. Following are the methods available in this interface:

  • void init(ServletConfig config)
  • void service(ServletRequest req, ServletResponse res)
  • void destroy()
  • ServletConfig getServletConfig()
  • String getServletInfo()

 

javax.servlet.ServletConfig Interface

 

The ServletConfig interface provides a standard abstraction for the servlet object to get environment details from the servlet container. Container implementing the ServletConfig object supports the following operations:

  • Retrieve the initialization parameters from xml file.
  • Retrieve the ServletContext object that describes the application’s runtime environment.
  • Retrieve the servlet name as configured in xml.

 

HttpServletRequest Interface

 

The HttpServletRequest interface is a subtype of ServletRequest interface. Implementation for this interface is provided by the servlet container. The HttpServletRequest interface object allows us to access the data available in the HTTP headers and HTTP requests. Following methods helps us to access the header information:

  • String getHeader(String)
  • Enumeration getHeaders(String)
  • Enumeration getHeaderNames()

 

Following methods helps us to access the path information:

  • String getContextPath()
  • String getServletPath()
  • String getPathInfo()
  • String getRequestURI()

 

HttpServletResponse Interface

 

The HttpServletResponse is a subtype of ServletResponse interface. Implementation for this interface is provided by the servlet container. The HttpServletResponse interface object let’s us to specify information that will be a part of the HTTP response headers or the HTTP response. Following methods helps us to manipulate the HTTP response headers:

  • addHeader(String name, String value)
  • containsHeader(String name)
  • setHeader(String name, String value)
  • setDateHeader(String name, long date)
  • addIntHeader(String name, int value)
  • addDateHeader(String name, long date)

 

Following table shows some of the header fields and the description of their value:

header-fields

 

Other methods provided by this interface are:

  • sendError(int)
  • sendError(int, String)
  • sendRedirect(String URL)

 

After the sendError() method is invoked, we cannot send any response messages to the client.

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me