1. What is different between web server and application server?
2. Which HTTP method is non idempotent?
3. What is the difference between GET and POST method?
4. What is MIME Type?
5. What is a web application and what is it?s directory structure?
6. What is a servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
7. What are the advantages of Servlet over CGI?
Platform Independence
Servlets are written entirely in java so these are platform independent. Servlets can run on any Servlet enabled web server. For example if you develop an web application in windows machine running Java web server, you can easily run the same on apache web server (if Apache Serve is installed) without modification or compilation of code. Platform independency of servlets provide a great advantages over alternatives of servlets.
Performance
Due 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 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.
Extensibility
Java 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 take all these advantages and can be extended from existing class to provide the ideal solutions.
Safety
Java provides very good safety features like memory management, exception handling etc. Servlets inherits all these features and emerged as a very powerful web server extension.
Secure
Servlets are server side components, so it inherits the security provided by the web server. Servlets are also benefited with Java Security Manager.
8. What are common tasks performed by Servlet Container?
Servlet containers or web container are also known as web container, for example Jakarta TomCat, WebLogic, JRun etc,in which the Web application containing the servlets is deployed invokes the servlets. Java servlets are not user-invokable applications. Some of the important tasks of servlet container are:
The web container providers implement most of the interfaces and classes of the Java Servlet API. When a servlet is being invoked, the Web container exchanges the incoming request information with the servlet, so that the servlet can analyze the incoming request, and generate responses dynamically. The Web container in turn interfaces with the Web server by accepting requests for servlets, transmitting responses back to the Web server.
Communication Support:- The servlet container provide the medium for inter servlet comunications and to the clints its browsers also. It perse the user request and generate the dynamic responses for the user. The all comlexity is get handled by the web servlet conainer. We do`t need to imlement the server socket layer to listen the clients requests. We need to focus on the business logics only.
Lifecycle and Resource Management: The life cyclee and all releated methods and instances get persed and handled by the web container by itself . The web container also provides utility like JNDI for resource pooling and management.
Multithreading Support: Container creates new thread for every request to the servlet and provide them request and response objects to process the request. So servlets are not initialized for each request. It shares the common one time memory for each request and saves time and memory.
JSP Support: JSPs doesn?t look like normal java classes. It looks like an HTML document but every JSP in the application is compiled by container and converted to Servlet and then container manages them like other servlets and get inter linked between the servlets by the web.xml deployer.
There are so many technologies for generating dynamic Web pages, such as CGI, NSAPI, ISAPI. from all of them the servlet framework provides a better abstraction of the HTTP request-response model by specifying a programming API for encapsulating requests, responses, sessions, beans etc.
Servlet instances can persist that much long across client requests, servlets have all the advantages of the Java programming language so the server is not constantly spawning external processes. Java servlet-based applications can be deployed on any Web server.
9. What are the phases of servlet life cycle?
Servlet class loading : For each servlet defined in the deployment descriptor of the Web application, the servlet container locates and loads a class of the type of the servlet. This can happen when the servlet engine itself is started, or later when a client request is actually delegated to the servlet.
Servlet instantiation : After loading, it instantiates one or more object instances of the servlet class to service the client requests.
Initialization (call the init method) : After instantiation, the container initializes a servlet before it is ready to handle client requests. The container initializes the servlet by invoking its init() method, passing an object implementing the ServletConfig interface. In the init() method, the servlet can read configuration parameters from the deployment descriptor or perform any other one-time activities, so the init() method is invoked once and only once by the servlet container.
Request handling (call the service method) : After the servlet is initialized, the container may keep it ready for handling client requests. When client requests arrive, they are delegated to the servlet through the service() method, passing the request and response objects as parameters. In the case of HTTP requests, the request and response objects are implementations of HttpServletRequest and HttpServletResponse respectively. In the HttpServlet class, the service() method invokes a different handler method for each type of HTTP request, doGet() method for GET requests, doPost() method for POST requests, and so on.
Removal from service (call the destroy method) : A servlet container may decide to remove a servlet from service for various reasons, such as to conserve memory resources. To do this, the servlet container calls the destroy() method on the servlet. Once the destroy() method has been called, the servlet may not service any more client requests. Now the servlet instance is eligible for garbage collection
First request is made.
Server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Administrator manually loads.
Server shuts down.
Administrator manually unloads.
The life cycle of a servlet is controlled by the container in which the servlet has been deployed.
10. What are life cycle methods of a servlet?
init() :-methods used to initialized resources used in servlet .
service():- method called always when other request come.
destroy():- method called only once and used to destroy resources which goes for garbage collected and finalized.