#2 Servlets
#2 Servlets
#2 Servlets
3. N-Tier Architecture:
➢ Extends the three-tier architecture by adding more layers (e.g., additional application servers)
to improve scalability and manageability.
➢ Example: Large-scale enterprise applications with multiple application servers handling
different types of business logic.
HTTP defines a set of request methods to indicate the desired action to be performed for a given
resource. Although they can also be nouns, these request methods are sometimes referred to as HTTP
verbs. Each of them implements a different semantic, but some common features are shared by a
group of them: e.g. a request method can be safe, idempotent, or cacheable.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
HTTP response status codes indicate whether a specific HTTP request has been successfully completed.
Responses are grouped in five classes:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Web Services
Web services are software systems designed to allow for interoperable communication and interaction
over a network, typically the internet. They facilitate the exchange of data and functionalities between
different applications or systems, making it possible for them to work together. Web services follow
specific communication protocols and use standard formats like XML or JSON for data exchange.
There are several types of web services, with two fundamental categories being:
1. SOAP (Simple Object Access Protocol) Web Services:
➢ SOAP is a protocol for exchanging structured information in web services.
➢ It uses XML for message formatting and relies on other protocols like HTTP and SMTP for
message negotiation and transmission.
➢ It defines a set of rules for structuring messages, including headers and bodies.
2. RESTful Web Services (Representational State Transfer):
➢ REST, or Representational State Transfer,
➢ is an architectural style for designing networked applications.
➢ A RESTful API (Application Programming Interface) is a set of rules and conventions for building
and interacting with web services.
➢ It typically uses standard HTTP methods like GET, POST, PUT, DELETE to perform operations on
resources, and it relies on stateless communication, meaning each request from a client
contains all the information needed to understand and fulfill that request.
➢ JSON is commonly used for data interchange in REST APIs.
➢ It often utilizes JSON or XML for data representation.
By understanding and leveraging the Eclipse Jakarta Project, you can build powerful, scalable, and
maintainable enterprise applications in Java.
Apache Tomcat 10.1 is an open-source implementation of the Jakarta Servlet, Jakarta Server Pages
(JSP), and WebSocket technologies, developed and maintained by the Apache Software Foundation.
Tomcat 10.1 provides a "pure Java" HTTP web server environment for Java code to run in, adhering to
the latest Jakarta EE standards.
### Key Features of Tomcat 10.1
1. Jakarta Servlet 6.0: Supports the latest features and updates in the Jakarta Servlet API, which is used
to create dynamic web applications.
2. Jakarta Server Pages (JSP) 3.1: Supports the latest JSP technology, allowing the creation of
dynamically generated web pages based on HTML, XML, or other document types.
3. Jakarta Expression Language (EL) 5.0: Provides a way to easily access application data stored in
JavaBeans components.
4. WebSocket 2.1: Supports WebSocket technology for creating bi-directional communication channels
over a single, long-lived connection, useful for real-time applications.
5. JSP Tag Library API 3.1: Supports the use of custom tags in JSP pages, which can encapsulate complex
server-side logic into simple, reusable tags.
### New Features and Improvements in Tomcat 10.1
1. Jakarta EE 9+ Compliance: Tomcat 10.1 is fully compliant with Jakarta EE 9 and beyond, which
involves a package namespace change from `javax` to `jakarta`.
2. Enhanced Security: Improved security features to protect web applications against various types of
attacks and vulnerabilities.
3. Performance Improvements: Optimizations to enhance the performance and efficiency of the server,
particularly under high load conditions.
4. Modernized Codebase: Updates and modernizations to the codebase to ensure compatibility with
the latest Java versions and standards.
5. Support for New HTTP/2 Features: Enhanced support for HTTP/2, which includes multiplexing of
streams, header compression, and server push capabilities.
### Deployment and Use Cases
1. Web Application Hosting: Tomcat 10.1 is widely used to host web applications written in Java. It is
particularly popular for enterprise-level applications.
2. Development and Testing: Developers use Tomcat to test Java Servlets and JSPs during the
development process due to its simplicity and ease of configuration.
3. Microservices and Cloud Deployments: Tomcat is lightweight and can be easily deployed in cloud
environments and microservices architectures.
### Configuration and Administration
- Server Configuration: Configured through XML configuration files such as `server.xml` and `web.xml`.
- Deployment: Web applications are typically packaged as WAR (Web Application Archive) files and
deployed to the `webapps` directory.
- Management: Comes with a web-based administration interface for managing deployed applications,
configuring server settings, and monitoring server status.
The Servlet API is a set of Java interfaces and classes that provide a standardized way to create and
manage web components on a server, such as servlets and filters. These components can handle
requests and generate responses, typically in the context of web applications. The Servlet API is part
of the Jakarta EE platform (previously Java EE).
https://jakarta.ee/specifications/servlet/5.0/apidocs/jakarta/servlet/servletcontext
## What is ServletContext?
`ServletContext` is an interface in the Jakarta Servlet 6.0 API that provides a servlet with
information about its environment. It represents the web application running on the servlet
container and allows servlets to interact with their context, sharing information and resources
among servlets within the same application.
## Use of ServletContext in an Application
The `ServletContext` object is created by the servlet container when the web application is
deployed and remains available to all servlets in that application throughout its lifecycle. It
provides methods to interact with the web application as a whole, including access to initialization
parameters, resources, and attributes shared among servlets.
### Key Uses:
1. Initialization Parameters: Retrieve initialization parameters that are common to the entire web
application.
2. Resource Access: Access web application resources, such as files and directories.
3. Attribute Sharing: Store and retrieve attributes that are shared among servlets within the same
web application.
4. Logging: Write log messages to the servlet container's log.
5. Request Dispatching: Forward or include requests to other resources within the web application.
Key Uses:
Logging and Auditing: Log request and response information.
Security: Implement authentication and authorization.
Data Compression: Compress response data.
Input Validation: Validate request parameters before they reach the servlet.
Response Modifications: Modify response data before it is sent back to the client.
2. `RequestDispatcher.forward()`
➢ Server-Side Forward: `forward()` forwards the request from one servlet or JSP to another
resource (servlet, JSP, or HTML file) on the server side. The browser is unaware that the forward
has taken place, so the URL in the address bar does not change.
➢ Same Request: The original request and response objects are passed along to the forwarded
resource, preserving any data that was in the request.
➢ Same Domain: You can only forward requests to resources within the same web application.
➢ No New Browser History Entry: Since this is a server-side operation, no new entry is added to
the browser history.
Example:
RequestDispatcher dispatcher = request.getRequestDispatcher("/anotherResource");
dispatcher.forward(request, response);
By using these techniques, web applications can maintain user state and provide a consistent and
personalized experience.