Servelets

SERVELET INTERFACE01

What is Servlet interface?

The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or , more commonly by extending a class that implements it.

SESSION

What is session?

A session refers to all the requests that a single client might make to a server in the course of viewing any pages associated with a given application. Sessions are specific to both the individual user and the application. As a result, every user of an application has a separate session and has access to a separate set of session variables.

WAR LIFE

What is the directory structure of a WAR file?

SERVELET CODE

How the typical servlet code look like ?

What is preinitialization of a servlet?

A container does not initialize the servlets as soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.

How can an existing session be invalidated?

An existing session can be invalidated in the following two ways: • Setting timeout in the deployment descriptor: This can be done by specifying timeout between the tags as follows:

10

This will set the time for session timeout to be ten minutes.

• Setting timeout programmatically: This will set the timeout for a specific session. The syntax for setting the timeout programmatically is as follows: public void setMaxInactiveInterval(int interval) The setMaxInactiveInterval() method sets the maximum time in seconds before a session becomes invalid. Note :Setting the inactive period as negative(-1), makes the container stop tracking session, i.e, session never expires. More >

How can the session in Servlet can be destroyed?

An existing session can be destroyed in the following two ways: • Programatically : Using session.invalidate() method, which makes the container abonden the session on which the method is called. • When the server itself is shutdown.

.A client sends requests to two different web components. Both of the components access the session. Will they end up using the same session object or different session ?

Creates only one session i.e., they end up with using same session . Sessions is specific to the client but not the web components. And there is a 1-1 mapping between client and a session.

What is servlet lazy loading?

• A container doesnot initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. • The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. • The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.

What is Servlet Chaining?

Servlet Chaining is a method where the output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser.