Skip to main content

Servlet API


Servlet API specification provides all the interfaces and classes to be used by the developer to create servlet programs and by container to provide run time environment to execute that servlet. Servlet is the specifications that define and describes the systematic and semantic approach to develop servlet class, which can be developed by the developer, which can be instantiated and executed by runtime environment by providing required implementation.

Servlet API describes and defines the contracts between a servlet class and the runtime environment.

The word framework is used many times which specifies that servlet api is the set of predefined interfaces and classes having some special methods where you will put your codes to execute within that method. Servlet class is instantiated and run by container, it does nothing itself.

This API defines the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.

There are two packages contain all the required interface and classes necessary to build servlet program.

1.2           javax.servlet

The javax.servlet package contains all classes and interfaces those all used to build servlet having no dependency on networking protocol.

1.2.1      Interfaces

Table 1
1.          Filter
It is used to perform filtering tasks on either the request to a resource or on the response from a resource, or both.
2.          FilterChain
The object of FilterChain is provided by the servlet container that provides next invocation chain of a filtered request for a resource.
3.          FilterConfig
It is used by a servlet container to pass information to a filter during initialization.
4.          RequestDispatcher
Implementation’s object of this interface sends requests to any resource (such as a servlet, HTML file, or JSP file) on the server.
5.          Servlet
Defines methods that all servlets must implement.
6.          ServletConfig
It is used by a servlet container to pass information to a servlet during initialization.
7.          ServletContext
The implemented object of ServletContext interface is used by the container to share some common elements within a web application, for all servlet and all client.
8.          ServletContextAttributeListener
Implementations this interface is used to observe the events of ServletContext when some attributes are added or removed.
9.          ServletContextListener
Implementations this interface is used to observe the events of ServletContext when it is created, destroyed etc.
10.       ServletRequest
Container implements this interface to create the request object to provide client request information to a servlet.
11.       ServletRequestAttributeListener
A ServletRequestAttributeListener can be implemented by the developer to listen events on ServletRequest object while adding and removing attributes.
12.       ServletRequestListener
A ServletRequestListener can be implemented by the developer to listen events occur when requests come in and out of scope in a web component.
13.       ServletResponse
Container implements this interface to create the request object to assist a servlet in sending a response to the client.
14.       SingleThreadModel
Deprecated. As of Java Servlet API 2.4, with no direct replacement.


1.2.2      Classes

Table 2
       1-       GenericServlet
Implementers Servlet interface and used to create protocol-independent servlet.
       2-       ServletContextAttributeEvent
Used by container to create an object created when ServletContext’s attributes are changed, for notifications about changes to the attributes of the servlet context of a web application.
       3-       ServletContextEvent
Used by container to create an object when Servlet Context of a web application is changed.
       4-       ServletInputStream
Use for reading binary data from a client request, including an efficient readLine method for reading data one line at a time.
       5-       ServletOutputStream
Used for sending binary data to the client.
       6-       ServletRequestAttributeEvent
Object of this class is created when attributes from the request object are added or removed or changed.
       7-       ServletRequestEvent
This is event object created when to notification about request lifecycle.
       8-       ServletRequestWrapper
This is concrete implementation of ServletRequest interface that is used to create ServletRequest object manually
       9-       ServletResponseWrapper
This is concrete implementation of ServletResponse interface that is used to create ServletResponse object manually

1.3             javax.servlet.http

This package contains all classes and interfaces to create the servlet program that follow http protocol rules and architecture.

1.3.1      Interfaces

Table 3
HttpServletRequest
This interface Extends the ServletRequest interface to provide request information for HTTP specific servlets.
HttpServletResponse
This interface Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.
HttpSession
Object of HttpSession implementation provides mechanisms to trace the user across multiple requests. And provides the convenient way to store the information of user.
HttpSessionActivationListener
This is the object that is notified by the container when sessions is activated and that session is passivated (deactivated).
HttpSessionAttributeListener
This interface is used to build session attribute listener. Object of that class will be notified when attributes of session will be changed.
HttpSessionBindingListener
Causes an object to be notified when it is bound to or unbound from a session.
HttpSessionContext
Deprecated. As of Java(tm) Servlet API 2.1 for security reasons, with no replacement.
HttpSessionListener
Implementations of this interface are notified of changes to the list of active sessions in a web application.

1.3.2      Classes

Table 4
Cookie
Object of Cookie class represents  a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server.
HttpServlet
This is abstract class having super class as GenericServlet class that is subclassed by developer to create java servlet program.
HttpServletRequestWrapper
Provides an implementation of the HttpServletRequest interface that can be subclassed by developers to create HttpServ;etRequest Object that can be passed to doGet/doPost method.
HttpServletResponseWrapper
Provides an implementation of the HttpServletResponse interface that can be subclassed by developers to create HttpServ;etResponse Object that can be passed to doGet/doPost method.
HttpSessionBindingEvent
Instance of HttpSessionBindingEvent is used to notify the event related to session binding.
HttpSessionEvent
Instance of This class representing event notifications for changes to sessions within a web application.
HttpUtils
Deprecated. As of Java(tm) Servlet API 2.3. These methods were only useful with the default encoding and have been moved to the request interfaces.

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t