servlet interview question

Upload: budhc

Post on 06-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Servlet Interview Question

    1/7

    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.

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

    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

    TOP

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

    A: Cookies

    SSL sessions

  • 8/3/2019 Servlet Interview Question

    2/7

    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 preinitialization of a servlet?

    A: A container doesnot 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 specificationdefines 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.

    [ 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 ] TOP

    Q: What is the difference between ServletContext and ServletConfig?

    A: ServletContext: Defines a set of methods that a servlet uses to communicate with its servlet

    container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.The

    ServletContext object is contained within the ServletConfig object, which the Web server providesthe servlet when the servlet is initialized

  • 8/3/2019 Servlet Interview Question

    3/7

    ServletConfig: The object created after a servlet is instantiated and its default constructor is read. It

    is created to pass initialization information to the servlet.

    [ Received from Sivagopal Balivada ]

    Question: What is a Servlet?Answer: Java Servlets areserver sidecomponents that provide a powerful mechanism for

    developing server side ofweb application. Earlier CGI was developed to provide server

    side capabilities to the web applications. Although CGI played a major role in the

    explosion of the Internet, its performance, scalability and reusability issues make it less

    than optimal solutions. Java Servlets changes all that. Built from ground up using Sun's

    write once run anywhere technology java servlets provide excellent framework for server

    side processing.

    Question: What are the types of Servlet?

    Answer: There are two types of servlets, GenericServlet and HttpServlet.

    GenericServlet defines the generic or protocol independent servlet. HttpServlet is subclass

    of GenericServlet and provides some http specific functionality like doGet and doPost

    methods.

    Question: What are the differences between HttpServlet and Generic Servlets?

    Answer:HttpServlet Provides an abstract class to be subclassed to create an HTTP

    servlet suitable for a Web site. A subclass ofHttpServlet must override at least one

    method, usually one of these:

    doGet, if the servlet supports HTTP GET requests doPost, for HTTP POST requests doPut, for HTTP PUT requests doDelete, for HTTP DELETE requests init and destroy, to manage resources that are held for the life of the servlet getServletInfo, which the servlet uses to provide information about itself

    There's almost no reason to override the service method. service handles standard

    HTTP requests by dispatching them to the handler methods for each HTTP request type

    (the doXXXmethods listed above). Likewise, there's almost no reason to override the

    doOptions and doTrace methods.

    GenericServlet defines a generic, protocol-independent servlet. To write an HTTP servlet

    for use on the Web, extend HttpServlet instead.

    GenericServlet implements the Servlet and ServletConfig interfaces.

    GenericServlet may be directly extended by a servlet, although it's more common to

    extend a protocol-specific subclass such as HttpServlet.

    GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle

    methods init and destroy and of the methods in the ServletConfig interface.

    GenericServlet also implements the log method, declared in the ServletContext

    interface.

    To write a generic servlet, you need only override the abstract service method.

    Question: Differentiate between Servlet and Applet.

    Answer: Servlets are server side components that executes on the server whereas applets

    http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/
  • 8/3/2019 Servlet Interview Question

    4/7

    are client side components and executes on theweb browser. Applets have GUI interface

    but there is not GUI interface in case of Servlets.

    Question: Differentiate between doGet and doPost method?

    Answer: doGet is used when there is are requirement of sending data appended to a query

    string in the URL. The doGet models the GET method of Http and it is used to retrieve the

    info on the client from some server as a request to it. The doGet cannot be used to send too

    much info appended as a query stream. GET puts the form values into the URL string.

    GET is limited to about 256 characters (usually a browser limitation) and creates really

    ugly URLs.

    POST allows you to have extremely dense forms and pass that to the server without clutter

    or limitation in size. e.g. you obviously can't send a file from the client to the server via

    GET. POST has no limit on the amount of data you can send and because the data does not

    show up on the URL you can send passwords. But this does not mean that POST is truly

    secure. For real security you have to look into encryption which is an entirely different

    topic

    Question: What are methods of HttpServlet?

    Answer: The methods of HttpServlet class are :

    * doGet() is used to handle the GET, conditional GET, and HEAD requests

    * doPost() is used to handle POST requests

    * doPut() is used to handle PUT requests

    * doDelete() is used to handle DELETE requests

    * doOptions() is used to handle the OPTIONS requests and

    * doTrace() is used to handle the TRACE requests

    Question: What are the advantages of Servlets over CGI programs?

    Answer: Java Servlets have a number of advantages over CGI and other API's. They are:

    1. Platform IndependenceJava Servlets are 100% pure Java, so it is platform independence. It can run on any

    Servlet enabledweb server. For example if you develop an web application in

    windows machine running Java web server. You can easily run the same onapache

    web server(if Apache Serve is installed) without modification or compilation of code.

    Platform independency of servlets provides a great advantage over alternatives of

    servlets.

    2. PerformanceDue to interpreted nature of java, programs written in java are slow. But the java

    servlets runs very fast. These are due to the way servlets run on web server. For any

    program initialization takes significant amount of time. But in case of servlets

    initialization takes place very first time it receives a request and remains in memory

    till times out or server shut downs. After servlet is loaded, to handle a new request it

    simply creates a new thread and runs service method of servlet. In comparison to

    traditional CGI scripts which creates a new process to serve the request. This intuitive

    method of servlets could be use to develop high speed data driven web sites.

    3. ExtensibilityJava Servlets are developed in java which is robust, well-designed and object oriented

    language which can be extended or polymorphed into new objects. So the java

    servlets takes all these advantages and can be extended from existing class the providethe ideal solutions.

    http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/http://www.roseindia.net/interviewquestions/servlet/
  • 8/3/2019 Servlet Interview Question

    5/7

    4. SafetyJava provides a very good safety features like memory management, exception

    handling etc. Servlets inherits all these features and emerged as a very powerful web

    server extension.

    5. SecureServlets are server side components, so it inherits the security provided by the webserver. Servlets are also benefited with Java Security Manager.

    6. Whats the advantages using servlets over using CGI?Servlets provide a way to generate dynamic documents that is both easier to write and faster to run.

    Servlets also address the problem of doing server-side programming with platform-specific APIs: they

    are developed with the Java Servlet API, a standard Java extension.

    7. What are the general advantages and selling points of Servlets?A servlet can handle multiple requests concurrently, and synchronize requests. This allows servlets to

    support systems such as online

    real-time conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be

    used to balance load among several servers that mirror the same content, and to partition a single logical

    service over several servers, according to task type or organizational boundaries.8. Which package provides interfaces and classes for writing servlets? javax9. Whats the 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 such as HttpServlet.Servlets > Generic Servlet >

    HttpServlet > MyServlet.

    The Servlet interface declares, but does not implement, methods that manage the servlet and its

    communications with clients. Servlet writers provide some or all of these methods when developing a

    servlet.

    10. When a servlet accepts a call from a client, it receives two objects. What are they?ServletRequest (which encapsulates the communication from the client to the server) andServletResponse (which encapsulates the communication from the servlet back to the client).

    ServletRequest and ServletResponse are interfaces defined inside javax.servlet package.

    11. What information does ServletRequest allow access to?Information such as the names of the parameters passed in by the client, the protocol (scheme) being

    used by the client, and the names

    of the remote host that made the request and the server that received it. Also the input stream, as

    ServletInputStream.Servlets use the input stream to get data from clients that use application protocols

    such as the HTTP POST and GET methods.

    12. What type of constraints can ServletResponse interface set on the client?It can set the content length and MIME type of the reply. It also provides an output stream,

    ServletOutputStream and a Writer throughwhich the servlet can send the reply data.

    13. Explain servlet lifecycle?Each servlet has the same life cycle: first, the server loads and initializes the servlet (init()), then the

    servlet handles zero or more client requests (service()), after that the server removes the servlet

    (destroy()). Worth noting that the last step on some servers is done when they shut down.

    14. How does HTTP Servlet handle client requests?An HTTP Servlet handles client requests through its service method. The service method supports

    standard HTTP client requests by dispatching each request to a method designed to handle that request.

    1) Explain in detail about a servlet?

    A Java servlet can be thought as an Applet but this runs on the server without the front side. Thisis used in applications where there is need for functionality of the web server and it is also used to

  • 8/3/2019 Servlet Interview Question

    6/7

    access the existing business applications. It removes the limitations caused due to CGI. Servlet isplatform and server independent which makes it much more popular to use.

    2) State some of the uses of Java servlet?Java servlet is used for many purposes. Being platform and server independent it is used for widevariety of purposes. They can provide secured access to web based data. Type safety and RAD

    based features help in productivity. It can be used to extend the functionality present in webservers.

    3) What services can be obtained by implementing Java Servlets?These are the following services which can be obtained by using Java servlets they are: - Multi user services can be provided by organization using Java servlets to their clients. They can also serve static web pages by making use of HTTP/ (s) protocols. Search engines and semi custom applications can make use of servlets (web based entry, etc).

    4) Should the user or client be in the same software language in their applications?Servlets are programmed in Java and the user need not be in the same language to access orimplement the features of Servlets. They are used in middle tier which provides them a flexibilityto be clients for other applications and the client who is accessing the servlet need not have the

    same language as the servlet.

    5) Is load balancing possible with a servlet?Load balancing is possible with the use of servlets. A servlet can forward requests to other serverswhich can drastically reduce load. This technique can reduce load by mirroring the same contentamong several servers. A single logical service can be partitioned among several servers i.e.,routing according to task type.

    6) Explain about the service method?Request and Response parameters are provided with the service methods. These parametersimplement encapsulation; they also give access to parameters which allows them to report errorsand status reports. Contrarily servlets retrieve parameters through an input stream and responsesare sent through output stream.

    7) Explain about the different usage modes present in Java servlet?Servlets can be used in many different modes they are: -1) Filter chains can be used to chain servlets together.2) They can be used to support HTTP protocols.3) They are the best replacement for CGI based applications4) Dynamic generation of content is possible with Servlets.

    8) State the different ways of loading a servlet?The different ways in which a servlet can be loaded are as follows: -1) Dynamic loading of servlet is always possible.2) Servers do provide an administrative option through which force loading and initialization ispossible.

    3) Servlets can also be loaded by Java class; they can be loaded from remote directories and localfile systems.

    9) Explain about servlet container?Servlet container supports servlet execution. Basic functionality of a web server and IDE of Javacan be used for better performance. Specific URL`s can be translated into servlet requests.Individual servlets get them selves registered with the container and this container holds andprovides information about the functionality of the servlet and the Url which can access theservlet.

    10) Explain about servletConfig and servletContext in applications?Servletcontext is used to obtain application level information and only one servletcontext can bepresent in one application. ServletConfig object is present for every servlet and it provides

    initialization parameters for every servlet.

  • 8/3/2019 Servlet Interview Question

    7/7

    11) Explain about Http specific servlets?Servlets using Http protocol support Http methods which includes GET, HEAD, POST, etc. Requestand response data are always provided in an MIME format. Data type is specified by the servletand data is also written in that format which gives greater flexibility in sending output in theformat requested.

    12) Explain about performance features of servlets?Creation of a new process for each and every request can be avoided and this is deemed to be thebiggest performance feature of servlets. Servlets run in parallel and in hand with the same processin the server. It provides greater flexibility and performance over CGI and fast CGI in an HTTPenvironment.

    13) Explain about JVM and Java servlets?Java servlets give greater performance for leveraging Java and related internet technologies.Throughput and response time can be improved by using Java servlets. With help from JVM Javaservlet programs take advantage of additional processors which will help them scale up operationsfrom entry level servers to main frame level applications.

    14) How does servlets handle data and give some examples of this feature?

    Servlets with precoded logic process data and they return reports in an appropriate format. Someof the examples of such formats are historical graphs and tables, web data, automated forecasts,etc. Administrative data such as addition, deletion of data can be performed with Servlets.

    15) What are the two different kinds of servlets used by collecting sites?There are two kinds of servlets used by collecting sites they are used to push and pull data. Onekind of servlet is used to push data to the collecting site and the other is used to pull data from theappropriate format. These formats can be clubbed with other servlets which further aggregate dataand can be used for large engineering projects.

    16) What are the various different input parameters which servlets accept?There are various different iinput parameters which servlets accept they are1) Request or an input stream from an applet or so.

    2) Request of the URL3) From different servlets or networks.4) Parameters passed from an HTML form.

    17) Explain about the security features of Servlets?Servlets cannot be trusted they have information about the clients. They have access to HTTPspecific authentication data and peer identities can be determined. Strong security policy supportis present in Java servlets. Access to network files and services needs to be restricted for a servlet.Security manager provided by Java can be used to control the level of security.