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
Contents
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:
Following are the classes available in this package:
Following are the exception classes available in this package:
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:
Following are the classes available in this package:
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:
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, 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.
Leave a Reply