Important Web Design and Development Short Questions
Chapter: 27
Question 1:
What is the difference between GenericServlet and HttpServlet?
Answer:
GenericServlet is protocol-independent and used for general servlets, while HttpServlet extends GenericServlet to handle HTTP-specific requests. Use HttpServlet for creating servlets that respond to HTTP requests.
Chapter: 27
Question 2:
How do you handle GET requests in a servlet?
Answer:
To handle GET requests, override the doGet() method in HttpServlet. This method receives HttpServletRequest and HttpServletResponse objects and processes the request, typically sending a response back to the client.
Chapter: 27
Question 3:
What is the purpose of the web.xml file in a servlet application?
Answer:
The web.xml file acts as a deployment descriptor, defining servlet mappings and configurations. It specifies servlet names, classes, and URL patterns for accessing the servlets.
Chapter: 27
Question 4:
What is the role of the lib folder within the WEB-INF directory?
Answer:
The lib folder is used to store JAR files (libraries) needed by the web application, such as APIs required for functionalities like PDF generation.
Chapter: 27
Question 5:
How can you test if the directory structure of a Tomcat web application is correctly set up?
Answer:
Create a simple HTML file, place it in the main application directory, restart the Tomcat server, and access the file using a URL in the format http://localhost:8080/myapp/index.html to verify if it loads correctly.
Chapter: 27
Question 6:
What is the doPost() method used for in a servlet?
Answer:
The doPost() method is used to handle POST requests, where data is sent in the body of the request. Override this method in HttpServlet to process form submissions or other data sent via POST requests.
Chapter: 28
Question 7:
What are the three main stages in the lifecycle of a servlet?
Answer:
The three main stages in the lifecycle of a servlet are Initialize, Service, and Destroy. During initialization, the init() method is called once. The service() method handles client requests, and the destroy() method is invoked to clean up resources before the servlet is unloaded.
Chapter: 28
Question 8:
When is the init() method called in a servlet?
Answer:
The init() method is called once when the servlet is first loaded into memory. It is used for one-time initialization tasks, such as loading resources or setting parameters. This method is not called for each client request but only when the servlet instance is created.
Chapter: 28
Question 9:
Describe the purpose of the service() method in a servlet.
Answer:
The service() method is responsible for processing client requests. It is called every time a request is received, and a new thread is created to handle the request. Depending on the request type (GET or POST), it delegates the request to the appropriate method, such as doGet() or doPost().
Chapter: 28
Question 10:
What happens during the destroy() stage of a servlet?
Answer:
During the destroy() stage, the servlet is unloaded from memory. The destroy() method is called to release any resources acquired by the servlet, such as closing database connections or stopping background threads. This method is invoked before the servlet is removed or when the server shuts down.