jump to navigation

Servlet Q’s July 21, 2005

Posted by Coolguy in J2SE.
trackback

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

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 sart with a “/” and are interpreted as

relative to curent context root.

Q

: Explain the directory structure of a web application.

A: The directory structure of a web application consists of two parts.

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

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 applicationor servlet container’s version. Every web application has one

and only one ServletContext and is accessible to all active resource of that

application.

Q: What is preinitialization of a servlet?

A: 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

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

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

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: