Untitled Document
Untitled Document
A Servlet is a Java program that runs on a web server or application server, handling
requests and responses in a web-based environment. They are used to extend the capabilities
of web servers by generating dynamic content (e.g., HTML, XML, JSON) in response to HTTP
requests.
1. The client (usually a web browser) sends an HTTP request to the server.
2. The request is passed to the servlet container, which decides which servlet should
handle the request.
3. The servlet processes the request, which may involve interacting with databases,
performing calculations, etc.
4. The servlet generates an HTTP response (usually in HTML or other formats).
A servlet goes through a specific life cycle controlled by the servlet container. The life cycle is
managed via a set of predefined methods in the javax.servlet.Servlet interface. The
container handles the creation, initialization, service, and destruction of a servlet. Below are the
key phases of the servlet life cycle:
1. Loading and Instantiating the Servlet: When a request is made for the servlet, the
Servlet container loads and instantiates the servlet if it is not already in memory.
The servlet is loaded based on the URL pattern specified in the web.xml configuration
file or annotations.
Key method: init(): This method is called once when the servlet is first loaded into memory.
It is used to initialize the servlet (e.g., setting up resources, database connections, etc.).
● The init() method is called only once when the servlet is first created. It is used to
perform any setup operations, like reading configuration parameters, setting up
database connections, etc.
● The servlet container passes a ServletConfig object to this method, which contains
configuration information for the servlet.
● After the servlet is initialized, the servlet container invokes the service() method each
time a request is made to the servlet.
● The service() method is responsible for processing the request and generating an
appropriate response.
4. Response Generation:
● The servlet generates an appropriate response (usually HTML, JSON, XML, etc.) that is
sent back to the client’s browser using the HttpServletResponse object.
● The response may involve setting response headers, content type, and writing the
output to the response’s output stream.
● The destroy() method is called when the servlet is about to be removed from memory.
This can happen when the server is shutting down or when the servlet is being unloaded
for other reasons.
● This method is used to clean up resources like closing database connections, releasing
file handles, etc.
● After the servlet has been destroyed, the servlet container unloads it from memory. This
frees up the resources used by the servlet.
Servlets: Methods
Servlets are Java classes used to handle requests and generate responses in a web
application. The HttpServlet class, which is a subclass of the GenericServlet class, is the
most commonly used servlet for handling HTTP requests.
When you extend HttpServlet, you can override various methods to handle different types of
HTTP requests (such as GET, POST, PUT, DELETE, etc.). Below are the key methods in a
servlet:
1. init() method
● Purpose: This method is called once when the servlet is first loaded into memory. It is
used to perform initialization tasks, such as setting up database connections or reading
configuration data.
● Usage: The init() method is called automatically by the servlet container when the
servlet is loaded, and it should only be used for initialization tasks.
2. service() method
● Purpose: The service() method is called each time a request is made to the servlet.
It is responsible for handling client requests and generating responses. The servlet
container invokes this method when an HTTP request is made.
3. doGet() method
● Purpose: This method is called when the servlet receives an HTTP GET request. It is
used to process data sent by the client, such as query parameters.
● Usage: You override this method to process the GET request and generate a response
(like HTML or JSON).
4. doPost() method
● Purpose: This method is called when the servlet receives an HTTP POST request. It is
typically used to handle form submissions or when the client sends data to the server.
5. doPut() method
● Purpose: This method is invoked when the servlet receives an HTTP PUT request. PUT
is used to update data on the server.
6. doDelete() method
● Purpose: This method is invoked when the servlet receives an HTTP DELETE request,
which is used to delete data from the server.
● 7. destroy() method
● Purpose: This method is called just before the servlet is destroyed (when the servlet
container unloads the servlet). It is used to release resources like closing database
connections, file handles, etc.
● Usage: It is used for clean-up tasks and should release any resources the servlet has
acquired during its life cycle.
Java Server Pages (JSP)
Java Server Pages (JSP) is a technology that helps software developers create dynamically
generated web pages based on HTML, XML, or other document types. JSP is part of Java EE
(Enterprise Edition) and allows the separation of business logic and presentation.
JSP files are similar to HTML files but can contain Java code within special tags. These files
have a .jsp extension and are compiled by the servlet container into servlets before they are
executed.
2. The JSP file is processed by the server, which compiles it into a servlet.
3. The servlet generates HTML or other content and sends it back to the client.
4. JSP can also interact with JavaBeans, servlets, and other server-side technologies to
generate dynamic content.
JSP allows embedding Java code inside HTML using special tags.
●
out.println(message);
%>
Expressions: Used to output the value of a Java expression directly to the client.
<%= "Hello, World!" %>
●
Declarations: Used to declare variables or methods that can be used in the JSP page.
<%! int counter = 0; %>
●
Action Tags: Used for various tasks like forwarding requests, including other resources, etc.
<jsp:forward page="otherPage.jsp" />
●
JSP Standard Tag Library (JSTL): Provides standard actions for common tasks like iteration,
conditionals, etc. Example of using JSTL to display a list:
<c:forEach var="item" items="${itemsList}">
<p>${item}</p>
</c:forEach>
●
JSP Lifecycle:
1. Translation: When the JSP file is first requested, the servlet container translates it into a
servlet. This servlet is stored and used for subsequent requests.
4. Request Processing: The service() method (or equivalent) is invoked to handle
requests.
6. Destruction: When the servlet is destroyed (e.g., when the container is shutting down),
the destroy() method is called.
server-side processing with Java:
1. Servlets:
● Servlets are Java programs that handle HTTP requests and generate responses.
They are used to process data, interact with databases, and generate dynamic content.
Key methods:
init(): Initializes the servlet.
doGet() / doPost(): Handle HTTP GET and POST requests.
destroy(): Cleans up resources when the servlet is destroyed.
2. JavaServer Pages (JSP):JSP allows embedding Java code into HTML using special
tags.It separates business logic (usually in servlets) from presentation (JSP).The servlet
container converts JSP files into servlets for processing.
● Spring MVC is a powerful framework for building Java web applications using the MVC
(Model-View-Controller) pattern.
It handles requests, processes business logic, and returns views.
It integrates well with other technologies like Hibernate for database access.
4. Hibernate:
● It eliminates the need to write raw SQL and automates CRUD operations.
● REST services allow clients to interact with your app using standard HTTP methods like
GET, POST, PUT, and DELETE.
In short, Java provides various ways to handle server-side processing, such as using Servlets,
JSP, Spring MVC, and Hibernate, depending on the requirements of the application.
AngularJS Expressions (Quick Overview)
AngularJS expressions allow you to bind data from the model (JavaScript) to the view (HTML).
They are written within {{}} and can perform simple operations.
Key Points:
● Used for: Displaying dynamic content, performing calculations, and calling functions.
Displaying Data:
<h1>{{ name }}</h1> <!-- Output: John -->
1.
Mathematical Expressions:
<h2>{{ 5 + 3 }}</h2> <!-- Output: 8 -->
2.
Function Calls:
<h2>{{ sum(5, 3) }}</h2> <!-- Output: 8 (from a controller function) -->
Using Filters:
<h2>{{ 12345.6789 | number:2 }}</h2> <!-- Output: 12,345.68 -->
Conditional Expressions:
<h2>{{ 10 > 5 ? 'Yes' : 'No' }}</h2> <!-- Output: Yes -->
Conclusion:
AngularJS simplifies the development of dynamic and interactive web applications with features
like two-way data binding, directives, dependency injection, and a powerful MVC architecture. It
is widely used for building single-page applications (SPAs).(Note: AngularJS is the original
version of Angular. Angular 2+ is the modern version, which is not backward compatible with
AngularJS.)
Data Binding in AngularJS
○ Both the model and the view are synchronized. When the model changes, the
view updates automatically, and when the view changes (e.g., user input), the
model is updated.
If the input changes, the name variable is updated, and the <h1> tag reflects the
change immediately.
○ The model updates the view but not vice versa. This is used when you need to
display data but don’t need user input to modify the model.
If message changes in the controller, the <h1> element will update automatically,
but changes in the view won't affect message.
Example:
<span ng-bind="userName"></span>
DOM (Document Object Model)
The DOM is a programming interface for web documents. It represents the structure of a web
page as a tree of objects, where each object corresponds to an element or attribute in the page.
Key Points of the DOM: 1. Tree Structure: The DOM represents the HTML document as a
tree, where each node represents an element, attribute, or piece of text.
Root: The document itself is the root.
Nodes: Every HTML tag, text, and attribute is a node in the DOM.
2. Manipulation: The DOM allows you to:
Access: Retrieve elements by their ID, class, tag, etc.
Modify: Change the content or structure of the document (add, remove, or update elements).
Create: Add new elements or attributes dynamically.
Delete: Remove elements or attributes.
DOM Methods: