Java-Servlet



1. What are Servlets?  

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.



2. What are the advantages of servlets over CGI?  

Servlets offer several advantages in comparison with the CGI.

  • Performance is significantly better.
  • Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.
  • Servlets are platform-independent because they are written in Java.
  • Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.
  • The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.


3. What are the major tasks of servlets?  

Servlets perform the following major tasks:
  • Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
  • Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
  • Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
  • Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
  • Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.


4. Explain servlet life cycle.  

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.

  • The servlet is initialized by calling the init () method.
  • The servlet calls service() method to process a client's request.
  • The servlet is terminated by calling the destroy() method.
  • Finally, servlet is garbage collected by the garbage collector of the JVM.



5. When init() method of servlet gets called?  

The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.



6. When service() method of servlet gets called?  

Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.



7. When service() method of servlet gets called?  

Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.



8. When destroy() method of servlet gets called?  

The destroy() method is called only once at the end of the life cycle of a servlet.


9. What is static in java?  

Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object.
A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.


10. What is final class?  

A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).


11. What if the main() method is declared as private?  

The program compiles properly but at runtime it will give "main() method not public." message.


12. What if the static modifier is removed from the signature of the main() method?  

Program compiles. But at runtime throws an error “NoSuchMethodError “


13. What if I write static public void instead of public static void?  

Program compiles and runs properly.


14. What if I do not provide the String array as the argument to the method?  

Program compiles but throws a runtime error "NoSuchMethodError".


15. What is the first argument of the String array in main() method?  

The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.


16. If I do not provide any arguments on the command line, then the String array of main() method will be empty or null?  

It is empty. But not null


17. How can one prove that the array is not null but empty using one line of code?  

Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.


18. What environment variables do I need to set on my machine in order to be able to run Java programs?  

CLASSPATH and PATH are the two variables.


19. Can an application have multiple classes having main() method?  

Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned.
Hence there is not conflict amongst the multiple classes having main() method.


20. Can I import same package/class twice? Will the JVM load the package twice at runtime?  

One can import the same package or same class multiple times. Neither compiler nor JVM complains about it. And the JVM will internally load the class only once no matter how many times you import the same class.


Java Interview Question

.Net Interview Question

PHP Interview Question

AngularJS Interview Questions