Pr.Pg Next Pg

Servlet Interview Questions and Answers Part 9

 

80.How to rectify errors in java servlet while compilation?

  • A java servlet is harder to compile then any other java programs. To remove errors while compiling java following method can be used:
    1. Check and resolve the errors on top of the listing. These errors could be the reason for related errors and thus removing them will eradicate the related errors.
    2. Check if the compiler is choking on packages not built into java as these packages cannot be found since these packages canít be imported and the classes under these are not usable.

If there is any tell the compiler where to find these classes.
This will resolve the errors that occurred in compiling servlet.

 

81.What are the steps involved in placing a servlet within a package?

  • Packages are used to separate the servlets under a directory to eradicate confusion among programmers working simultaneously on creating servlets on the same server.

  • This can be done by the following steps:
    1. Sorting of files into different sub directories by matching the package name to the subdirectory name to organize the packages.
    2. After sorting the packages into subdirectories insert the package statement in the class file.
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class servlet1 extends HttpServlet {
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String docType =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
    "Transitional//EN\">\n";
    out.println(docType +
    "<HTML>\n" +
    "<HEAD><TITLE>Servlet test</TITLE></HEAD>\n" +
    "<BODY BGCOLOR=\"#FDF5E6\">\n" +
    "</BODY></HTML>");
    }
    }


82. What is the difference between the servlets and CGI programs?

1.Servlets provide the component bases services that are platform independent, whereas Common gateway interface (CGI) programs are used for the dynamic web content creation and to take the request and send them to the user.
2. Servlets performs better than CGI programs as they use the inbuilt API and keep the program size under control, whereas CGI suffer from the performance that is very low and it takes more time for the request to be answered.
3. Servlets are portable across all the platform and various web servers, whereas CGI are not portable to all the places due to increase in the size of the overall program.
4. Servlets allow the server-side programming to be written without any platform specification and by the use of APIs, whereas CGI is used to send and receive the request from the user.
5. Servlets are more efficient due to its lightweight use of java thread that handles the servlet requests, whereas the CGI is heavier due to the operating system process.

 

 

83. What is the use of Java Servlet API?

  • The servlet APIs represent the classes that are used to define an interface in between the web client and servlet.

  • API is used to request the server to pass the object to the server.

  • This servlet consists of the objects that produce the response that is passed to the client side.

  • The API consists of two packages that are used to support the generic protocol independent servlets like javax.servlet and javax.servlet.http this also include the specific support files to support the HTTP protocol and the properties are also provided in there.

  • The Java Servlet API is used to make it easy for the programmer to write the codes in an easy way and it also increase the overall performance of the system.

 

84.Why are HTTP servlets used in programming?

  • HTTP servlets define the servlets for the web to access the services using the HTTP protocol.

  • The servlet interface in this is having a class that consists of the centralized abstraction using the Java Servlet API.

  • The servlet class defines the methods that can be used for the communication between the clients.

  • HTTP is used as a simple and stateless protocol that allows the client to make the request and server responds to it when the transaction is done.

  • When a request is received from the client possibly a web browser than using the HTTP command a method will be called that allow the servers to take certain types of action on the particular request.

  • The URL of the document represents the version of the HTTP protocol that is being used in the program.

 

85.What are the steps that are involved in using the HttpServlet class?

  • The HttpServlet class is used to take the request and respond to it using the necessary services that exist.

  • The client sends a request to the servlet that is represented using the HttpServletRequest object.

  • This object allows the communication to be performed in between the client and the server.

  • It consists of all the messages that has to be transmitted for the communication.

  • The class consists of the information that is provided for the client environment and any data that is being sent from the client using the servlet.

  • The server responds using the object of HttpServletResponse.

  • This response can be dynamically generated and includes the HTML page with the data and its request that can be gathered from the other servlet.

 

86.What are the key methods that are involved in processing of HTTP servlets?

  • There are many methods that are involved and to successfully allow the client to get the response the subclass of HttpServlet should override at least one method out of doGet or doPost.

  • The GET requests are the common requests that are made by the browser when it request for the web pages.

  • The GET method gets activated when the user uses the URL and clicks on the link.

  • POST requests are used on the submission of HTML form that specifies the particular post request to be generated and handled.

  • This method allows the client to send the data that is of variable length to the Web server at one time. This allows the simple and easy posting of the confidential information like credit card password, etc.

 

87.What are the common methods that are included in the HTTP servlet class?

  • The common methods that are included in HTTP servlet class includes of doGet and doPost methods that are used mostly in all the code files.

  • These are the services that are implemented by the servlet as it allows different types of data to be displayed. The other methods that are used in the HTTP servlet class are:
    1. doPut method is used to put the HTTP requests. It is called by the server to allow the servlet to handle the requests. This operation allows the client to upload a file on the server or handle the file sending request by using FTP. It is used as:
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException,java.io.IOException
    2. doDelete method allows the deletion of the HTTP requests by the server. It is called by the server to perform the delete operation using which the client is allowed to remove the document from the web page that will remove it from the server as well. It is given as:
    protected void doDelete(HttpServletRequest req,HttpServletResponse resp) throws ServletException, java.io.IOException

 

 

88. What are the ways to handle multi-threading in Servlets?

  • Multi-threading is used with the processes to divide the tasks into multiple threads and share the resources between them.

  • It increases the speed and the performance of the system and the program that is in execution.

  • When there are requests coming at the same time to the servlet, then the server will handle the requests coming from each client by creating a new thread and inserting the request in it.

  • The model that is used consists of single thread per client/server request.

  • This allows the thread to be reused again in the case when the same client is trying to make again the request.

  • This multi-threading environment decreases the time delay using the API and decreases the delay in between the communication of the server and the client used for the requests.

 

89.What are the steps that are required to handle the multi-threading?

  • To handle the multi-threading environment the steps are required to be followed like
    The servlet has to be loaded by the the web server.

  • The web application server consists of one instance that allows the handling of the object of the servlet one at a time.

  • This object remains persistent throughout the lifecycle of the servlet.
    The web browser that acts as a client request the services to be produced by the server or using the servlets.

  • The server at that time creates the thread that is used to handle each request, by ignoring the instance object.

  • The individual thread is used to access the variables that are initialized when the servlet is loaded on the server to respond to the clientís request.Individual threads handle their own requests that are sent back to the client who has asked for the services and requested for it.

 

90. What is the process to implement doGet and doPost methods?

  • doGet methods are the service methods that is included in the servlets.

  • This allow the servlet to handle the GET request that is being processed by the client.

  • This overrides the method to support the GET request that automatically supports the HTTP head request that is given in the no body in the response and included in the header fields.

  • It is being implemented as:
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException
    It is used to read the request data and provide the response of it by putting the solution in the header.

  • It uses the responseís writer or the object stream object that provides the response to the client.

  • This is safe to use and can be safely repeated in case of any other request. .
    doPost is used to handle a POST request that is also given at the time of filling up the form or any other action that is related to the user submission.

  • This method allows the client to send the data of any length to the web server and that is also at single time. To read the request data the response headers are included that takes the response writer class to write that uses the output stream object.

  • It uses the function of PrintWriter object that returns the response to set the content type of accessing the object. It is given as:
    doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException


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-9.php on line 446

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-9.php on line 446

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-9.php on line 452

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-9.php on line 452