Sunteți pe pagina 1din 4

Overview of Servlet

Servlets provide a Java-based solution used to address the problem currently


associated with doing server-side programming, including insensible scripting
solutions, platform – specific APIs, and incomplete interfaces.
Servlets are objects that confirm to a specific interface that can be plugged
into a java – based server. Servlets are to the server-side what applets are to the
Client-side object bytecodes that can be dynamically loaded off the net. They differ
from applets in that they are faceless objects (without graphics or GUI
components). They serve as platform independent, dynamically loadable, pluggable
helper bytecode objects on the server side that can be used to dynamically extend
Server-side functionality.
For example, HTTP Servlet can be used to generate dynamic HTML content.
When you use Servlets to do dynamic content you get the following advantages.
1. They’re faster and cleaner than CGI scripts.
2. They use a standard API (the Servlet API).
3. They provide all the advantages of Java (run on a variety of servers
without needing to be rewritten.).

Attractiveness of Servlets

There are many features of Servlets that make them and attractive to use.
These include:
1. Easily configured using GUI based Admin tool.
2. Can be loaded and invoked from a local disk or remotely across the
network.
3. Can be linked together, or chained, so that one Servlet can call another
servlet or several servlets in sequence.
4. Can be called dynamically from within HTML, pages using server-side
include tags.
5. Are source-even when downloading across the network, the servlet
security model and servlet sandbox protect your system from unfriendly
behavior.

The advantages of the Servlet API

One of the great advantages of the servlet API is, it is protocol independent. It
assumes nothing about:
1. The protocol being used to transmit on the net.
2. How it is loaded.
3. The server environment it will be running in.
These qualities are important, because it allows the Servlet API to be
embedded in many different kinds of servers. There are other advantages to the
Servlet API as well. These include:
It’s extensible – you can inherit all your functionality from the base classes
made available to you.
It’s simple small and easy to use.
JAVA SERVLET FEATURES

The java server provides several important servlet features these include:
Loading and invoking servlets
Servlets can be loaded both locally and remotely.
Filters and Servlet chaining
The java server uses mime types to call servlets sequentially.
Server-Side Includes
Servlets can be nvoked from dynamic HTML documents using server side
include tags.
Replacing CGI scripts
Servlets are an effective substitute for CGI scripts, and provide faster and
cleaner why to generate dynamic documents.

INTERNAL SERVLETS:

The java TM Web Server TM Servlet architecture is very flexible and the
server takes advantage of this by dividing its work among several internal servlets.
These are:

• File servlet:
The file servlet provides the standard document serving capabilities of java
Web Server. This servlet includes a caching mechanism to speed up response times
for frequently accessed files.

• Invoker Servlet:
The purpose of this servlet is to invoke other servlets which are explicitly
requested by name, that is http://<serverhost name>/servlet/<Servlet name>.

• Server sides include servlet:


Servlets can be embedded with in html documents using the servlet tag.
When the server detects the servlet tag, it loads the servlet if necessary, invokes that
servlet, and sends the output of the servlet to the client at the point where the
servlet tag was embedded.

• Admin servlet:
The admin servlet facilitates administration of the Java Web Server through a
CGI front end of the Administration Tool.

• CGI servlet:
This servlet acts as a gateway for the CGI 1.1 interface. This servlet allows
any program that utilizes the CGI 1.1 standard to operate under JavaWer Server.

• Image Map Servlet:


This servlet implements server-side image maps utilizing an extension of the
standard NCSA map files.
Q: Explain the life cycle methods of a Servlet.

A: The javax.servlet.Servlet interface defines the three methods known as life-cycle


method.
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws
ServletException, IOException
public void destroy()
First the servlet is constructed, then initialized with the init() method.
Any request from client are handled initially by the service() method before
delegating to the doXxx() methods in the case of HttpServlet.

The servlet is removed from service, destroyed with the destroy() methid, then
garbaged collected and finalized.
TOP

Q: What is the difference between the getRequestDispatcher(String path)


method of javax.servlet.ServletRequest interface and
javax.servlet.ServletContext interface?
A: The getRequestDispatcher(String path) method of javax.servlet.ServletRequest
interface accepts parameter the path to the resource to be included or forwarded
to, which can be relative to the request of the calling servlet. If the path begins
with a "/" it is interpreted as relative to the current context root.

The getRequestDispatcher(String path) method of javax.servlet.ServletContext


interface cannot accepts relative paths. All path must start with a "/" and are
interpreted as relative to current context root.
TOP

Q: Explain the directory structure of a web application.

A: The directory structure of a web application consists of twoparts.


A private directory called WEB-INF
A public resource directory which contains public resource folder.

WEB-INF folder consists of


1. web.xml
2. classes directory
3. lib directory
TOP

Q: What are the common mechanisms used for session tracking?

A: Cookies
SSL sessions
URL- rewriting
TOP

Q: Explain ServletContext.

A: ServletContext interface is a window for a servlet to view it's environment. A


servlet can use this interface to get information such as initialization parameters
for the web application or servlet container's version. Every web application has
one and only one ServletContext and is accessible to all active resource of that
application.
TOP

Q: What is pre-initialization of a servlet?

A: A container doesn’t 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 <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 pre-initializing a servlet.
[ Received from Amit Bhoir ] TOP

Q: What is the difference between Difference between doGet() and


doPost()?
A: A doGet() method is limited with 2k of data to be sent, and doPost() method
doesn't have this limitation. A request string for doGet() looks like the following:
http://www.allapplabs.com/svt1?p1=v1&p2=v2&...&pN=vN
doPost() method call doesn't need a long text tail after a servlet name in a
request. All parameters are stored in a request itself, not in a request string, and
it's impossible to guess the data transmitted to a servlet only looking at a request
string.
[ Received from Amit Bhoir ] TOP

Q: What is the difference between HttpServlet and GenericServlet?

A: A GenericServlet has a service() method aimed to handle requests. HttpServlet


extends GenericServlet and adds support for doGet(), doPost(), doHead() methods
(HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).
Both these classes are abstract.
[ Received from Amit Bhoir ]

S-ar putea să vă placă și