Search This Blog

Thursday, November 19, 2009

Servlet Question and Answer

When I will prepare for technical interview, I have used to think about hints and opt technical terms.

So I have started preparing question while reading books. I will ask question my self and redo the answer until I get opt technical words.

This post all are my own preparation if any one find its use full I really deserve for that.

Visitors of my blogspot kindly add your valuable comments to improve my post.

Note:

If u find any answer is wrong let me know. So I can redefine the same.

  1. What is Servlet?
    • Servlet is a java program, It will run inside the web container
    • Servlet extends request/respond –oriented servers such as java oriented web server.
  2. Different way to session tracking?
    • Cookies
    • URL re-writings
    • Http session tracking
    • Hidden form
    • Session
  3. What mechanism is used by servlet container to maintain the session?
    • Session
    • Cookies
    • URL re-writing
  4. Difference between GET and POST?

No.

Get

Post

1

Data are submitted part of URL

Data are submitted inside body of servlet request.

2

Query max length is 255 character

No Limitation

3

Its not secure

Its secure.

  1. Session
    • Session is a object is used to track the information about the user with web application across multiple HTTP Request (State less).
    • Session is stored on web server.
  2. Servlet Mapping
    • Mapping defines the association between the URL pattern and Servlet
    • Mapping will be define in web.xml
    • Mapping is used to map the particular request to Servlet.
  3. Servlet Context:
    • It’s a servlet Object that contains information about web application and container.
    • Each application has only one servlet Context Object.
    • Uses for set the attribute that other servlet in the same context can use.
  4. Can we use constructer instead of Init():
    • Yes we can use constructer in Servlet
    • But we wont get servlet specific feature
    • So we can’t use servletcontext and servletconfig object in constructer.
  5. Life cycle method of the servlet:
    • Javax.servlet.Servlet interface providing 3 life cycle methods are
    • Public void init (ServletConfig config)- will be called when instance is created.
    • Public void service(ServletRequest req,ServletResponse res)-Every request is handled by service method only.
    • Public void distory()-when object is de-allocated from JVM.
  6. Pre Initialization of a servlet:
    • Process of loading servlet before any request comes in is called pre loading/pre initialization.
    • We have to include the element priority for every servlet in web.xml
  7. Difference between HttpServlet and Generic Servlet:

No.

Http Servlet

Generic Servlet

1

Parrent Class for Http Servlet is Generic Servlet

Parent class of HttpServlet is Servlet

2

Http Servlet support doget(),dopost(),doHead methods

Service method will handle all the request.

  1. Difference between ServletContext and ServletConfig:

No.

Servlet Context

Servlet Config

1

Set of methods support to communicate with servlet container

Its specific to the single servlet.

2

Uses for set the attribute that other servlet in the same context can use.

Its used to initialize the servlet specific value.

3

It’s created when servlet container created.

It’s created when specific class is loaded by container.

  1. Advantages and selling point of Servlet?

    • Servlet handle the multiple requests and synchronize the request.
    • This allows servlet to support online real time conferencing.
    • Servlet forward the request to the other servlet.
    • So servlet can be used to balance the load among several servers that mirror the same content.
  1. what are the Information servletRequest object contain:
    • parameter passed by the client
    • name of the remote host
    • the protocol
  2. Type of constraints send on the client : by servlet Response interface

    • Content length
    • Content type
    • Servlet Writer, Servlet output stream and a writer through which the servlet can send the data.

  1. Context Initialization Parameter:
    • It’s specified in web.xml under the element.
    • This is a parameter for the whole application not specific for Servlet/JSP.
    • It will be only accessed by servlet context object.

  1. Servlet Chaining:

    • Two are more servlet co-operate in servicing a single request.

  1. Difference between Servlet Context and Page Context?
    • ServletContext –Gives information about Servlet container
    • PageContext-Gives information about request
  2. Difference:
    • Request.getRequestDispacher()-Need to give relative path.
    • Context.getRequestDispacher()-Need to give absolute path.
  3. Difference:
    • RequestDispacher()-Direct with request and Response object.
    • sendRequest()-direct with new request and Response object.

3 comments: