Sie sind auf Seite 1von 8

21.

What are the differences between the ServletConfig interface and the ServletContext
interface?

ServletConfig ServletContext

The ServletConfig interface is implemented by


the servlet container in order to pass
A ServletContext defines a set of methods that
configuration information to a servlet. The
a servlet uses to communicate with its servlet
server passes an object that implements the
container.
ServletConfig interface to the servlet's init()
method.

There is one ServletContext for the entire


There is one ServletConfig parameter per
webapp and all the servlets in a webapp share
servlet.
it.

The param-value pairs for ServletConfig object The param-value pairs for ServletContext
are specified in the <init-param> within the object are specified in the <context-param>
<servlet> tags in the web.xml file tags in the web.xml file.

22.What's the difference between forward() and sendRedirect() methods?

forward() sendRedirect()

A redirect is a two step process, where the web


A forward is performed internally by the
application instructs the browser to fetch a
servlet.
second URL, which differs from the original.

The browser is completely unaware that it has The browser, in this case, is doing the work
taken place, so its original URL remains intact. and knows that it's making a new request.

Any browser reload of the resulting page will A browser reloads of the second URL ,will not
simple repeat the original request, with the repeat the original request, but will rather fetch
original URL the second URL.

Both resources must be part of the same


This method can be used to redirect users to
context (Some containers make provisions for
resources that are not part of the current
cross-context communication but this tends not
context, or even in the same domain.
to be very portable)

Because this involves a new request, the


Since both resources are part of same context, previous request scope objects, with all of its
the original request context is retained parameters and attributes are no longer
available after a redirect.
(Variables will need to be passed by via the
session object).

redirect is marginally slower than a forward,


Forward is marginally faster than redirect.
since it requires two browser requests, not one.

23.What is the difference between the include() and forward() methods?

include() forward()

The RequestDispatcher include() method


inserts the the contents of the specified The RequestDispatcher forward() method
resource directly in the flow of the servlet is used to show a different resource in place of
response, as if it were part of the calling the servlet that was originally called.
servlet.

The forwarded resource may be another


If you include a servlet or JSP document, the
servlet, JSP or static HTML document, but the
included resource must not attempt to change
response is issued under the same URL that
the response status code or HTTP headers, any
was originally requested. In other words, it is
such request will be ignored.
not the same as a redirection.

The forward() method is often used where a


The include() method is often used to
servlet is taking a controller role; processing
include common "boilerplate" text or template
some input and deciding the outcome by
markup that may be included by many servlets.
returning a particular response page.

24.What's the use of the servlet wrapper classes??

The HttpServletRequestWrapper and HttpServletResponseWrapper classes are


designed to make it easy for developers to create custom implementations of the servlet request
and response types. The classes are constructed with the standard HttpServletRequest and
HttpServletResponse instances respectively and their default behaviour is to pass all method
calls directly to the underlying objects.

26.What is a deployment descriptor?

A deployment descriptor is an XML document with an .xml extension. It defines a component's


deployment settings. It declares transaction attributes and security authorization for an enterprise
bean. The information provided by a deployment descriptor is declarative and therefore it can be
modified without changing the source code of a bean.
The JavaEE server reads the deployment descriptor at run time and acts upon the component
accordingly.

27.What is the difference between the getRequestDispatcher(String path) method of


javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?

ServletRequest.getRequestDispatcher(Strin ServletContext.getRequestDispatcher(Strin
g path) g path)

The getRequestDispatcher(String path)


method of javax.servlet.ServletRequest
The getRequestDispatcher(String path)
interface accepts parameter the path to the
method of javax.servlet.ServletContext
resource to be included or forwarded to, which
interface cannot accept relative paths. All path
can be relative to the request of the calling
must start with a / and are interpreted as
servlet. If the path begins with a / it is
relative to current context root.
interpreted as relative to the current context
root.

28.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.

29.What is the <load-on-startup> element?

The <load-on-startup> element of a deployment descriptor is used to load a servlet file when
the server starts instead of waiting for the first request. It is also used to specify the order in
which the files are to be loaded.

. 30.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.
31.What is Session Tracking?

Session tracking is a mechanism that servlets use to maintain state about a series of requests from
the same user (that is, requests originating from the same browser) across some period of time.

32.What is the need of Session Tracking in web application?

HTTP is a stateless protocol i.e., every request is treated as new request. For web applications to
be more realistic they have to retain information across multiple requests. Such information
which is part of the application is reffered as "state". To keep track of this state we need session
tracking.

33.What are the types of Session Tracking ?

Sessions need to work with all web browsers and take into account the users security
preferences. Therefore there are a variety of ways to send and receive the identifier:

URL rewriting : URL rewriting is a method of session tracking in which some extra data
(session ID) is appended at the end of each URL. This extra data identifies the session.
The server can associate this session identifier with the data it has stored about that
session. This method is used with browsers that do not support cookies or where the user
has disabled the cookies.

Hidden Form Fields : Similar to URL rewriting. The server embeds new hidden fields
in every dynamically generated form page for the client. When the client submits the
form to the server the hidden fields identify the client.

Cookies : Cookie is a small amount of information sent by a servlet to a Web browser.


Saved by the browser, and later sent back to the server in subsequent requests. A cookie
has a name, a single value, and optional attributes. A cookie's value can uniquely identify
a client.

Secure Socket Layer (SSL) Sessions : Web browsers that support Secure Socket Layer
communication can use SSL's support via HTTPS for generating a unique session key as
part of the encrypted conversation.
34.How do I use cookies to store session state on the client?

In a servlet, the HttpServletResponse and HttpServletRequest objects passed to method


HttpServlet.service() can be used to create cookies on the client and use cookie information
transmitted during client requests. JSPs can also use cookies, in scriptlet code or, preferably,
from within custom tag code.

To set a cookie on the client, use the addCookie() method in class HttpServletResponse.
Multiple cookies may be set for the same request, and a single cookie name may have
multiple values.
To get all of the cookies associated with a single HTTP request, use the getCookies()
method of class HttpServletRequest

35.What are some advantages of storing session state in cookies?

Cookies are usually persistent, so for low-security sites, user data that needs to be stored
long-term (such as a user ID, historical information, etc.) can be maintained easily with
no server interaction.
For small- and medium-sized session data, the entire session data (instead of just the
session ID) can be kept in the cookie.

36.What are some disadvantages of storing session state in cookies?

Cookies are controlled by programming a low-level API, which is more difficult


to implement than some other approaches.
All data for a session are kept on the client. Corruption, expiration or purging of cookie
files can all result in incomplete, inconsistent, or missing information.
Cookies may not be available for many reasons: the user may have disabled them, the
browser version may not support them, the browser may be behind a firewall that filters
cookies, and so on. Servlets and JSP pages that rely exclusively on cookies for client-side
session state will not operate properly for all clients. Using cookies, and then switching to
an alternate client-side session state strategy in cases where cookies aren't available,
complicates development and maintenance.
Browser instances share cookies, so users cannot have multiple simultaneous sessions.
Cookie-based solutions work only for HTTP clients. This is because cookies are a feature
of the HTTP protocol. Notice that the while package javax.servlet.http supports
session management (via class HttpSession), package javax.servlet has no such
support.

37.What is URL rewriting?


URL rewriting is a method of session tracking in which some extra data is appended at the end of
each URL. This extra data identifies the session. The server can associate this session identifier
with the data it has stored about that session.

Every URL on the page must be encoded using method HttpServletResponse.encodeURL().


Each time a URL is output, the servlet passes the URL to encodeURL(), which encodes session
ID in the URL if the browser isn't accepting cookies, or if the session tracking is turned off.
E.g., http://abc/path/index.jsp;jsessionid=123465hfhs

Advantages

URL rewriting works just about everywhere, especially when cookies are turned off.
Multiple simultaneous sessions are possible for a single user. Session information is local
to each browser instance, since it's stored in URLs in each page being displayed. This
scheme isn't foolproof, though, since users can start a new browser instance using a URL
for an active session, and confuse the server by interacting with the same session through
two instances.
Entirely static pages cannot be used with URL rewriting, since every link must be
dynamically written with the session state. It is possible to combine static and dynamic
content, using (for example) templating or server-side includes. This limitation is also a
barrier to integrating legacy web pages with newer, servlet-based pages.

DisAdvantages

Every URL on a page which needs the session information must be rewritten each time a
page is served. Not only is this expensive computationally, but it can greatly increase
communication overhead.
URL rewriting limits the client's interaction with the server to HTTP GETs, which can
result in awkward restrictions on the page.
URL rewriting does not work well with JSP technology.
If a client workstation crashes, all of the URLs (and therefore all of the data for that
session) are lost.

38.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 <session-timeout>tags as follows:

<session-config>
<session-timeout>10</session-timeout>
</session-config>

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.

39.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.

40.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.

41.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 <load-on-startup> 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.

42.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.
43.How are filters?

Filters are Java components that are used to intercept an incoming request to a Web resource and
a response sent back from the resource. It is used to abstract any useful information contained in
the request or response. Some of the important functions performed by filters are as follows:

Security checks
Modifying the request or response
Data compression
Logging and auditing
Response compression

Filters are configured in the deployment descriptor of a Web application. Hence, a user is not
required to recompile anything to change the input or output of the Web application.

44.What are the functions of an intercepting filter?

The functions of an intercepting filter are as follows:

It intercepts the request from a client before it reaches the servlet and modifies the
request if required.
It intercepts the response from the servlet back to the client and modifies the request if
required.
There can be many filters forming a chain, in which case the output of one filter becomes
an input to the next filter. Hence, various modifications can be performed on a single
request and response

45.What are the functions of the Servlet container?

The functions of the Servlet container are as follows:

Lifecycle management : It manages the life and death of a servlet, such as class loading,
instantiation, initialization, service, and making servlet instances eligible for garbage
collection.
Communication support : It handles the communication between the servlet and the
Web server.
Multithreading support : It automatically creates a new thread for every servlet request
received. When the Servlet service() method completes, the thread dies.
Declarative security : It manages the security inside the XML deployment descriptor
file.
JSP support : The container is responsible for converting JSPs to servlets and for
maintaining them.

Das könnte Ihnen auch gefallen