Pr.Pg Next Pg

Servlet Interview Questions and Answers Part 10

 

91.Write a program to show the functionality of doGet and doPost method?

  • doGet method reads the request data that is submitted by the user and sends the reply back to the client whose request is to processed.

  • The parameters that are used in this method consists of strings, integers and appropriate exception need to be catched when using the method. getWriter method is used to provide the response object that provide different parameters to be passed to the client.

  • The program consists of the doPost calling the doGet method where the program can alter the data that is provided in response.

  • The program is written as:
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    PrintWriter out = response.getWriter();
    printHeader(out, response);
    for (int k = 0; k < answers.length; k++)
    {
    try
    {
    userAnswers[k] = (Integer.parseInt (request.getParameter("answer"+k)));
    }
    catch (NumberFormatException e)
    {
    userAnswers[k] = 0;
    }
    }
    submit = request.getParameter("submit");
    printQuiz(out);
    printClosing(out);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    this.doGet(request, response);
    }

 

92. How can the referrer and the target URLs be used in servlet?

  • The servlet first look for the referrer is defined as the parameter.

  • The servlet after checking set it as an empty string. Referrer is not used after this and it is logged to a file or the database after accumulating the session tracking information on the user.

  • The servlet checks for the target that is specified and throws an exception called as IllegalArgumentException.

  • The informaiton of the target can be checked to see the starting of the Http service that does not exist at the time of test. The code shows the referrer and the target URLs be used as as:
    public void getURLs(HttpServletRequest request)
    {
    referrer = request.getParameter("referrer");
    if (referrer == null || 0 == referrer.length())
    {
    referrer = new String("");
    }
    target = request.getParameter("target");

    // If no target specified, raise an error
    if (target == null || target.equals(""))
    {
    throw new IllegalArgumentException();
    }}}

 

93.Taking servlet out of service (Destroying servlet):

  • When a servlet is no longer required the destroy method() is called by servlet container.

  • This method removes the servlet.

  • Once removed there are no request sent to the servlet and all resources associated with it are released

 

93.How can I send user authentication information while making URL Connection?

  • You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.

 

94. Can we use the constructor, instead of init(), to initialize servlet?

  • Yes , of course you can use the constructor instead of init().

  • There's nothing to stop you. But you shouldn't.

  • The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig.

  • That no longer applies, but servlet containers still will only call your no-arg constructor. So you won't have access to a ServletConfig or ServletContext.

 

95.Explain ServletContext.

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

 

96.What is preinitialization of a servlet?

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

 

97.Can we call destroy() method inside the init() method is yes what will happen?

  • Yes we can call like this but  if we have not override this method container will call the default method and nothing will happen.after calling this if any we have override the method then the code written inside is executed.

 

98.How can we refresh servlet on client and server side automatically?

  • On client side we can use Meta http refresh and server side we can use server push.

 

99.How can you get the information about one servlet context in another servlet?

  • In context object we can set the attribute which we want on another servlet and we can get that attribute using their name on another servlet.

Context.setAttribute (“name”,” value”)

Context.getAttribute (“name”)

 

100.what is servlet collaboration?

  • Communication between two servlet is called servlet collaboration which is achieved by 3 ways.

1. RequestDispatchers include () and forward() method .

2. Using sendRedirect()method of Response object.

3. Using servlet Context methods


Pr.Pg border                                              Next Pg

Warning: include_once(bottompad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-10.php on line 331

Warning: include_once() [function.include]: Failed opening 'bottompad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-10.php on line 331

Warning: include_once(rightpad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-10.php on line 337

Warning: include_once() [function.include]: Failed opening 'rightpad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-10.php on line 337