Advanced Java & Servlet Interview Questions
1. What is JDBC in Java?
JDBC (Java Database Connectivity) is an API that allows Java applications to connect and interact
with databases.
2. What are the steps to connect a Java application to a database using JDBC?
1. Load Driver
2. Establish Connection
3. Create Statement
4. Execute Query
5. Close Connection
3. Difference between Statement and PreparedStatement?
PreparedStatement is precompiled and safer against SQL injection, while Statement is compiled at
runtime.
4. What is ResultSet in JDBC?
It represents the result set of a database query and allows iteration over query results.
5. What is connection pooling?
It is a technique to reuse database connections, improving performance and reducing resource
usage.
6. What is the role of DriverManager?
DriverManager is used to manage JDBC drivers and establish database connections.
7. What is CallableStatement?
It is used to execute stored procedures in the database.
8. What are types of JDBC drivers?
1. JDBC-ODBC bridge
2. Native API driver
3. Network Protocol driver
4. Thin driver (Pure Java)
9. What is the use of auto-commit in JDBC?
If auto-commit is true, each SQL statement is treated as a transaction and committed automatically.
10. How to handle exceptions in JDBC?
Using try-catch blocks and handling SQLException properly to manage database-related errors.
11. What is a Servlet?
A Servlet is a Java class used to handle HTTP requests and generate dynamic web content.
12. What is the life cycle of a Servlet?
1. init()
2. service()
3. destroy()
13. Difference between doGet() and doPost()?
doGet is used for fetching data (URL visible), doPost is used for sending data securely.
14. What is the web.xml file?
It is a deployment descriptor used to configure servlets and servlet-mapping in a web application.
15. How do you configure a servlet?
By defining servlet and servlet-mapping tags in web.xml or using @WebServlet annotation.
16. What is RequestDispatcher?
It is used to forward or include the request to another resource (HTML, JSP, or another servlet).
17. What is the difference between forward() and sendRedirect()?
forward() happens on server-side, sendRedirect() instructs browser to make a new request.
18. What are servlet attributes?
Attributes are objects used to share data between servlets during a single request or session.
19. What is the difference between ServletConfig and ServletContext?
ServletConfig is per servlet, ServletContext is shared across the entire application.
20. What are HTTPServlet and GenericServlet?
GenericServlet is protocol-independent; HttpServlet extends GenericServlet and supports HTTP.
21. What is a session in Servlets?
A session is a way to maintain data across multiple requests from the same user.
22. What is Cookie in Servlet?
A small piece of data stored on client's browser used for session tracking.
23. How do you handle exceptions in Servlets?
Using try-catch blocks or defining error-page in web.xml.
24. What is load-on-startup in web.xml?
It tells the container to load the servlet at the time of application startup.
25. Can a servlet have multiple URL mappings?
Yes, using multiple <url-pattern> tags in web.xml or multiple values in @WebServlet annotation.