Pr.Pg Next Pg

Servlet Interview Questions and Answers Part 8

 

71.Define Declaration.

  • Declaration is used to declare one or more variables or methods.

  • It is used in the JSP source file later on.

  • The declaration must be contained minimum one full declarative statement.

  • You can declare any number of variables or methods in one declaration, whereas semicolons separate them.

  • In the JSP file the declaration can be done in the scripting language .

  • This is included in the declaration section of the newly generated servlet.

 

72.What is a Scriptlet?

  • Scriptlet contains language statements, variable & expressions

  • These are valid in the page scripting language.

  • By using scriptlet tags we can declare variables for using later in the file.

  • By using scriptlet we can write expressions valid in the page scripting language .

  • In General a scriptlet may contain a java code which is valid inside a java method.

 

 

73.What are the different methods involved in generic servlet?

  • Generic servlet is a base class of servlet. It involves the following methods:
    1. init() method:- This method is called once by the servlet container in its lifecycle . It takes a ServletConfig object that contains the initialization parameters and configuration of the servlet.
    2. Service() method :- This method is defined as a public void service as ServletRequest “req” OR ServletResponse “res” which gives Servlet Exception, as IOException .If once the servlet starts getting requests, the service() method is called by the servlet container to respond to the requests.
    3. Getservlet config()- The method comprises of the initialization and the startup of the servlet. It is also responsible for returning the Server Config object.
    4. Getservlet info():- This method is defined as public String getServletInfo() .It is responsible for returning a string which is in the form of plain text and not any kind of markup.
    5. destroy():- This method is defined as public destroy().this method is called when want to close the servlet.

 

74.Define the life cycle of a servlets.

  • Among various other roles of the web container, the most important is to manage the life cycle of a servlet. The functionality of a servlet is managed by a well defined life cycle that states how it is loaded, Instantiated, initialized, handles client request and taken out of service.
    Servlet lifecycle thus can be divided into four parts:
    1. Loading and instantiation: In this the web container first loads the servlet class. The web container then creates an instance of this servlet. Loading of servlet is done during the startup when the first request is made, whereas the creation of instance can occur at startup or can be delayed till service to a request is required by the servlet.
    2. Initialization: After the creation of an instance the init() method is called by the servlet to initialise the servlet instance. The init() method must be called before any of the requests are serviced by the servlet. The parameters of initialization are passed to init() and persist till servlet is destroyed. If loaded successfully the servlet is available for service, if not then the servlet is unloaded by the servlet container.
    3. Handling requests: After initialization the servlet can be used to handle client requests. Requests are represented by request objects of type ServletRequest. A separate thread is created for each request.

 

75. How to generate the server side programming and the advantages of it over the other languages?

  • The server side programming is having lots of advantages over other programming languages that it is one of the efficient and faster methods to implement the server code on the remote machines. The advantages that are possible are as follows:

1. The programs are centralized on a single server and any of the remote machines can access the server programs.
2. The clients can add new functionalities to the server side without making any change from there side.
3. Adding of patches, switching to new database and migrating to new version, architecture and design is not dependent on the software and hardware capabilities thus making it more independent.
4. Service side applications can be used to manage issues related to enterprise application.
5. Server side programming is capable of generating dynamic and user band content also it is very portable.

 

76.What is the requirement of servlet config and servlet context implemented and how are they implemented?

  • The servlet config is used for initialization of a servlet. Using servlet config initialization parameters can be passed for the srevlet by making use of web xml.
    <servlet>
    <servlet-name>ServletConfigABC</servlet-name>
    <servlet-class>com.javapapers.ServletConfigABC</servlet-class>
    <init-param>
    <initparam-name>topic</initparam-name>
    <initparam-value>Difference between ServletConfig and ServletContext</initparam-value>
    </init-param>
    </servlet>

 

77.What are the objects involved when a servlet receives a call from client?

  • Servlets are designed to receive request or calls from clients. When a servlet receives a call from a client there are two objects involved in the process.

  • The objects involved the request received from the client and the response of servlet to the client.
    The 2 objects involved when a servlet receives a call from a client are :
    1. Servlet Request : The servlet request represents the request made by the client. It encapsulates communication from client side to server side. The servlet request checks the request and finds out which servlet shall respond to the request made by client.
    2. Servlet Response: The servlet response represents the response made by the servlet to the client. It assists the servlet in sending a response to the client.

 

 

78.How can a servlet be used to generate plain text instead of html?

  • A servlet generally generates html. To make a servlet generate plain text following method can be used:

  • Set content type response header by any of the method.

  • The example for producing text message ABCD using the HTML code inside the servlets.
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class ABCD extends HttpServlet {
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
    "Transitional//EN\">\n" +
    "<HTML>\n" +
    "<HEAD><TITLE>ABCD</TITLE></HEAD>\n" +
    "<BODY>\n" +
    "<H1>ABCD</H1>\n" +
    "</BODY></HTML>");

 

79.What is HTTP servlet? Explain with the help of an example.

  • HTTP servlet is an extension of the Generic servlets. It provides various methods such as doPut().

  • doGet() etc which are useful in determining the type of request being made and thus picking the right method.
    Example
    HttpServlet:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class ABCDServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    out.println("<HTML>");
    out.println(
    "<HEAD><TITLE>Title</TITLE></HEAD>");
    out.println(
    "<BODY><H1>ABCD!</H1><H6>Again.</H6></BODY></HTML>");
    }
    }


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-8.php on line 339

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-8.php on line 339

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-8.php on line 345

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-8.php on line 345