Ajp Qs 2
Ajp Qs 2
1. General Concepts
2. What are the features of Java that make it suitable for web applications?
Features: Platform independence (runs anywhere with JVM), built-in security (e.g.,
encryption), scalability (supports large-scale applications), and APIs like JDBC, Servlets, and
JSP for enterprise apps.
JDK: Includes tools for development (compiler, debugger). JRE: Provides runtime libraries for
executing Java programs. JVM: Converts bytecode to machine code for execution.
4. What are the key differences between Java SE and Java EE?
Java SE is the standard edition for standalone apps, while Java EE extends it for enterprise
needs, adding APIs for web services, Servlets, and EJBs.
JDBC is an API to connect Java applications to relational databases for executing SQL queries.
6. What are the main steps involved in establishing a database connection using JDBC?
• Close connections.
7. What is the difference between Statement and PreparedStatement?
Handle SQL errors using try-catch blocks, and log the error messages for debugging.
ResultSet: An object used to navigate and retrieve data returned by SQL queries.
3. Servlets
Servlets are server-side Java programs that process client requests (like HTTP GET/POST) and
generate responses dynamically (e.g., HTML or JSON).
12. What is the difference between doGet() and doPost() methods in Servlets?
doGet() is used to handle data retrieval (e.g., query parameters in URL). doPost() is used for
sending sensitive data (e.g., form submissions) in the request body.
13. How can you forward a request from one Servlet to another?
Manage sessions using cookies, URL rewriting, HttpSession objects, or hidden fields to store
and track user information.
4. JSP (JavaServer Pages)
JSP simplifies web development by embedding Java code into HTML to create dynamic
content.
JSP is mostly used for the view (UI), while Servlets handle logic (backend). JSPs are compiled
into Servlets internally.
17. Explain the use of JSP directives like <%@ page %> and <%@ include %>.
<%@ page %> sets page-level attributes (e.g., import libraries). <%@ include %> includes
static files at translation time.
Use <%@ include file="header.html" %> to include static files like headers or footers.
19. What are scriptlets in JSP, and how are they used?
Scriptlets are Java code written within JSP tags (<% %>). Example: <% int x = 10; %>.
5. Multithreading
Create threads by extending the Thread class or implementing the Runnable interface.
22. What is the difference between the Runnable interface and the Thread class?
Thread class ties thread behavior to the logic; Runnable separates thread creation from the
task logic.
23. What are thread priorities, and how do they affect execution?
Thread priorities (1-10) determine the relative importance of threads; higher priority threads
get preference.
Synchronization prevents race conditions by allowing only one thread to access a shared
resource at a time.
6. Networking
A simple client-server app uses ServerSocket (server-side) to accept connections and Socket
(client-side) to connect.
TCP: Reliable and connection-oriented (e.g., HTTP). UDP: Faster but connectionless (e.g.,
video streaming).
Java handles HTTP requests via HttpURLConnection for GET/POST requests to APIs or
websites.
JDBC program: Write a program to connect to a database, execute a query (e.g., SELECT *),
process the results, and close the connection.
Servlet example: Write a doGet() method to display "Hello, World!" in the browser.
32. How do you pass data between a JSP and a Servlet?
33. Explain how to retrieve data from a database and display it in a JSP page.
Fetch database rows via JDBC, loop through the ResultSet, and display data in an HTML
<table> in JSP.
34. How would you implement a login page using JSP and Servlets?
Use JSP for the UI (login form) and a Servlet for backend validation (check credentials in the
database).
Web application: Software hosted on a server, accessed via browsers using URLs.
WAR file: Web Application Archive that packages web apps (JSPs, Servlets, HTML, etc.) for
deployment.
MVC: A design pattern that separates business logic (Model), user interface (View), and
control flow (Controller).
39. What are Java Beans, and how are they used in JSP?
JavaBeans: Classes with properties (getters/setters) used for encapsulating data and sharing
it between JSP and Servlets.
40. What are some commonly used Java frameworks for web development?
Frameworks: Commonly used frameworks are Spring (MVC, DI), Hibernate (ORM), and
Struts (MVC).