0% found this document useful (0 votes)
3 views10 pages

Untitled Document

Servlets are Java programs that run on web servers to handle requests and generate dynamic content. They follow a specific life cycle managed by the servlet container, which includes methods like init(), service(), and destroy() for initialization, request handling, and cleanup. Java Server Pages (JSP) is a related technology that allows embedding Java code in HTML to create dynamic web pages, and AngularJS provides data binding features for building interactive web applications.

Uploaded by

sameer Shaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Untitled Document

Servlets are Java programs that run on web servers to handle requests and generate dynamic content. They follow a specific life cycle managed by the servlet container, which includes methods like init(), service(), and destroy() for initialization, request handling, and cleanup. Java Server Pages (JSP) is a related technology that allows embedding Java code in HTML to create dynamic web pages, and AngularJS provides data binding features for building interactive web applications.

Uploaded by

sameer Shaik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Servlets: Introduction

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.

How Servlets Work:

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).​

5.​ The response is sent back to the client.​

Life Cycle of a Servlet

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.).​

2. Initialization (init() method):

●​ 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.

3. Request Handling (service() method):

●​ After the servlet is initialized, the servlet container invokes the service() method each
time a request is made to the servlet.​

●​ The service() method receives two arguments:​


ServletRequest: Contains the client’s request (such as form data, query parameters).​
ServletResponse: Used to generate a response to be sent to the client.​

●​ 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.​

5. Servlet Destruction (destroy() method):

●​ 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.​

6. Unloading the Servlet:

●​ 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.​

●​ Signature: public void init() throws ServletException​

●​ 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.​

●​ Signature: public void service(ServletRequest req, ServletResponse


res) throws ServletException, IOException

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.​

●​ Signature: protected void doGet(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException​

●​ 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.​

●​ Signature: protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException​

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.​

●​ Signature: protected void doPut(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException​

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.​

●​ Signature: protected void doDelete(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException​

●​ 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.​

●​ Signature: public void destroy()​

●​ 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.

How JSP Works:

1.​ A client (browser) makes an HTTP request.​

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.​

Basic JSP Syntax:

JSP allows embedding Java code inside HTML using special tags.

Directives: Used to provide global settings for the JSP page.​



<%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>

●​

Scriptlets: Used to embed Java code inside a JSP page.​



<%

String message = "Hello, World!";

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.​

2.​ Compilation: The generated servlet is compiled into bytecode.​

3.​ Initialization: The servlet's init() method is called (if applicable).​

4.​ Request Processing: The service() method (or equivalent) is invoked to handle
requests.​

5.​ Response: The generated response is sent back to the client.​

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.​

3. Spring Framework (Spring MVC):

●​ 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:

●​ Hibernate is an ORM (Object-Relational Mapping) framework that simplifies database


interaction by mapping Java objects to database tables.​

●​ It eliminates the need to write raw SQL and automates CRUD operations.​

5. RESTful Services with JAX-RS:

●​ JAX-RS provides a way to create RESTful web services in Java.​

●​ 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:

●​ Syntax: {{ expression }}​

●​ Used for: Displaying dynamic content, performing calculations, and calling functions.​

●​ Restrictions: Cannot have loops or conditionals.​


Examples:

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

Data binding in AngularJS is a technique that allows automatic synchronization of data


between the model (JavaScript variables) and the view (HTML). It eliminates the need for
manually updating the view when the model changes.

Types of Data Binding in AngularJS:

1.​ Two-Way Data Binding:​

○​ 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.​

2.​ One-Way Data Binding:​

○​ 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.​

Key Directives for Data Binding:

1.​ ng-model (Two-Way Binding):​

○​ Binds an input field to a model.


○​
2.​ {{ expression }} (One-Way Binding):​

○​ Used to display data from the model in the view.​

3.​ ng-bind (One-Way Binding):​

○​ Binds data to an HTML element without using curly braces {{}}.​

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:

document.getElementById(): Gets an element by its ID.​



let element = document.getElementById('myElement');

document.querySelector(): Selects the first element that matches a CSS selector.​



let element = document.querySelector('.myClass');

document.createElement(): Creates a new HTML element.​



let newDiv = document.createElement('div');

element.appendChild(): Adds a new child element to a parent element.​



document.body.appendChild(newDiv);

element.innerHTML: Gets or sets the HTML content of an element.​



element.innerHTML = 'New content';

element.style: Modify the style of an element.​



element.style.color = 'red';:The DOM provides a structured way to interact with web pages
through JavaScript. It allows developers to access and manipulate the page's elements,
enabling dynamic changes, such as updating content or styles without reloading the page.

You might also like