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
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Servlets. No Comments on Servlet API
5
(1)

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

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


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.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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

Your email address will not be published. Required fields are marked *