Assignment 1-3 Java
Assignment 1-3 Java
• Types of Containers:
o Web Containers: Manage the execution of web components like servlets and JSPs.
o EJB Containers: Handle the execution of Enterprise JavaBeans, providing support for
transactions and remote method calls.
• Definition: The Servlet interface defines the methods that all servlets must implement.
• Methods Overview:
o init(): Called once by the servlet container when the servlet is first loaded, used to
perform any required initialization.
o service(): Invoked for each client request, used to process requests and generate
responses (like handling HTTP requests).
o destroy(): Called before the servlet is unloaded from memory to release resources
like file handles or database connections.
o getServletInfo(): Returns information about the servlet, such as version and author
details.
• Loading and Instantiation: The servlet is loaded by the web container when it is first
requested or during server startup.
• Initialization (init()): The init() method is called once for initialization tasks, such as setting up
resources.
• Request Handling (service()): The service() method is invoked for each request. It delegates
the request to appropriate doGet(), doPost(), etc., based on the request type.
• Destruction (destroy()): Before the servlet is removed from memory, the destroy() method is
called to clean up resources.
• Garbage Collection: The servlet object is garbage collected by the JVM after being destroyed.
4. JDBC Architecture
• Java Database Connectivity (JDBC): Allows Java applications to interact with databases.
• Components:
o JDBC Drivers: Act as a bridge between the application and the database.
• Layers:
JDBC drivers are categorized into four types based on their architecture and how they connect Java
applications to databases:
o Advantages:
o Disadvantages:
o Description: Uses native code libraries of the database to convert JDBC calls into
database-specific calls. The driver interacts with the database using the native
database client library.
o Advantages:
o Disadvantages:
o Advantages:
o Disadvantages:
o Description: Directly converts JDBC calls into the database-specific protocol using
pure Java. Does not require any native code, making it platform-independent.
o Advantages:
o Disadvantages:
• Components:
o Client: The user interface or application that interacts with the server. It handles the
presentation and user input.
o Server: The backend system (often a database server) that handles data storage,
processing, and retrieval.
• Workflow:
o The server processes the request and sends the response back to the client.
• Advantages:
o Performance: Direct communication between client and server can result in fast
responses.
• Disadvantages:
o Scalability Issues: As the number of clients increases, the server may become
overloaded, leading to performance degradation.
o Limited Flexibility: Adding new features may require changes to both client and
server.
• Class:
o Characteristics:
java
Copy code
class Car {
String model;
void drive() {
• Interface:
o Definition: A reference type in Java that is similar to a class but only contains method
signatures (no method implementations).
o Characteristics:
▪ Cannot have instance fields; can only have static and final variables.
o Example:
java
Copy code
interface Drivable {
void drive();
• Comparison:
o Classes support inheritance, but interfaces are used for abstraction and defining
behaviors that multiple classes can share.
o Example: Java's Collections Framework provides an API for working with data
structures like lists, sets, and maps.
ASSIGNMENT-2
1. RequestDispatcher Interface
• Purpose: It enables forwarding the request to another resource or including content from
another resource in the response.
• Methods:
• Definition: Cookies are small pieces of data stored on the client's machine to maintain state
across multiple requests.
o setMaxAge(int expiry): Sets the maximum age of the cookie (in seconds).
o setDomain(String domain): Specifies the domain for which the cookie is valid.
• Use of Cookies:
o Tracking User Behavior: Tracks user activity across websites for analytics.
3. HttpSession
• Definition: The HttpSession interface provides a way to identify a user across multiple
requests and store information about that user.
• Features:
o Attributes Handling: Allows storage of objects (user data, shopping carts, etc.) in the
session.
o Methods:
• Usage: Commonly used for user login, shopping cart management, and tracking user-specific
data.
4. GlassFish Server
• Definition: GlassFish is an open-source application server for Java EE. It provides a server
environment for deploying and running Java EE applications.
• Features:
o Support for Java EE Standards: Fully supports the latest Java EE specifications.
• Definition: A web server is a server software that serves web pages to clients over HTTP or
HTTPS. It handles requests from clients (browsers) and provides responses.
o Nginx:
o Lighttpd:
o Tomcat:
o Syntax: scheme:[//authority]path[?query][#fragment].
o Example: https://example.com/index.html.
• Key Differences:
o Save the File: Write the file to the desired location on the server.
• Libraries for Easier Handling: Use libraries like Apache Commons FileUpload for managing file
uploads.
• Security Considerations: Validate file types and restrict file sizes to prevent malicious
uploads.
• How It Works:
o Channels and Buffers: Uses channels for data transmission and buffers for temporary
storage.
o Selectors: Allows monitoring multiple channels for events (read, write) without
blocking.
• Advantages:
• Disadvantages:
These answers are detailed enough to be suitable for a 6-mark question, providing clear explanations
and examples where applicable.
ASSIGNMENT-3
1. Translation Phase: JSP is translated into a servlet. The JSP file is converted into a Java
servlet source file (.java).
2. Compilation Phase: The generated servlet source file is compiled into a bytecode
(.class file).
3. Loading and Instantiation: The compiled servlet class is loaded into the JVM, and an
instance of the servlet is created.
4. Initialization (jspInit() method): Called once when the servlet instance is created,
similar to init() in servlets.
6. Destruction (jspDestroy() method): Called when the JSP is being unloaded from
memory, similar to destroy() in servlets.
• Diagram Representation:
rust
Copy code
JSP File --> [Translation] --> Servlet (.java) --> [Compilation] --> Bytecode (.class)
--> [Loading & Instantiation] --> Initialization (jspInit) --> Request Handling (_jspService)
• Nature:
• Compilation:
• Ease of Use:
• Usage:
• Lifecycle Methods:
o Readability: JSTL tags make code more readable by separating logic from HTML.
o Maintainability: Easier to maintain because the code is less cluttered with Java.
o Standardization: Provides a set of standard tags, reducing the need for custom tag
libraries.
o Reusability: JSTL can be used across different JSPs without needing to write the
same code repeatedly.
o Evaluates immediately.
EL Implicit Objects:
o Example: ${param.username}
o Example: ${header["User-Agent"]}
o Example: ${sessionScope.user}
Advantages:
This version is shorter and highlights the core concepts and examples clearly.
• 1. request:
• 2. response:
• 3. session:
• 4. application:
o Represents the ServletContext for the JSP.
• 5. out:
• 6. config:
• 1. Directive Tags:
o Types: <%@ page %>, <%@ include %>, <%@ taglib %>.
• 2. Scripting Tags:
o Types: <% ... %> (scriptlet), <%= ... %> (expression), <%! ... %> (declaration).
• 3. Action Tags:
• 4. Custom Tags:
• 1. HttpJspPage:
o Key Methods:
▪ jspInit(): Called once during the JSP initialization phase, similar to the init()
method of a servlet.
▪ jspDestroy(): Called when the JSP is unloaded, just like the destroy() method
in a servlet.
o Usage: Provides the standard methods that every JSP must implement after being
compiled into a servlet.
• 2. JspWriter:
o Buffering Levels:
o Methods:
• 3. PageContext:
o Description: An implicit object that provides access to various elements like request,
response, session, application, and configuration.
o Main Methods:
• 4. JspApplicationContext:
o Description: Provides a context for the entire web application. It is used for
configuring EL resolvers and other global settings.
o Usage: Often utilized in custom tag development for registering EL resolvers and
integrating with other frameworks.
• 5. JspFactory:
o Description: A factory class for creating instances of JSP implicit objects like
PageContext and JspWriter.
o Methods: