java unit 3
java unit 3
Q.1 What is a Session? How to set value and get value from session?
> In Java, a session refers to a mechanism for storing and managing data specific to a user or a
conversation between a client and a server. Sessions are commonly used in web development to
store user information, login details, and other temporary data.
In Java, you can use the HttpSession interface to work with sessions. Here's how to set and get values
from a session:
import javax.servlet.http.HttpSession;
session.setAttribute("username", "johnDoe");
The classloader is responsible to load the servlet class. The servlet class is loaded when the first
request for the servlet is received by the web container.
The web container creates the instance of a servlet after loading the servlet class. The servlet
instance is created only once in the servlet life cycle.
The web container calls the init method only once after creating the servlet instance. The init
method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet
interface. Syntax of the init method is given below:
The web container calls the service method each time when request for the servlet is received. If
servlet is not initialized, it follows the first three steps as described above then calls the service
method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once.
The syntax of the service method of the Servlet interface is given below:
The web container calls the destroy method before removing the servlet instance from the service. It
gives the servlet an opportunity to clean up any resource for example memory, thread etc. The
syntax of the destroy method of the Servlet interface is given below:
Here are some key points about the Filter API in Java:
Filter: A filter is an object that can transform the request or response headers and content before
and after a servlet is executed.
1. *Filter Interface*: The javax.servlet.Filter interface must be implemented to create a filter. This
interface has three main methods:
- destroy(): Called by the web container to indicate that the filter is being taken out of service.
2. *Filter Chain*: Filters can be configured in a chain, where each filter can perform its task and pass
the request and response to the next filter in the chain.
4. *Common Uses*: Filters are commonly used for tasks such as:
- Data compression
1. Reusability: Filters can be used to apply a specific behavior to multiple servlets or JSPs without
duplicating code. This makes it easier to maintain and update the application.
2. Modularity: Filters are independent components that can be added or removed from an
application without affecting the application's core functionality. This provides developers with the
flexibility to add or remove features as required.
3. Centralized control: Filters provide a centralized point of control for common tasks such as
authentication, logging, and caching. This makes it easier to manage and maintain the application.
4. Improved performance: Filters can be used to cache responses, compress data, or perform other
optimizations that can improve the performance of the application.
5. Security: Filters can be used to enforce security policies such as authentication, authorization, and
encryption. This helps to ensure that sensitive data is protected from unauthorized access.
Q.6 What is the difference between doGet() and doPost() methods in Servlets?
Q.7 Discuss the use of GET and POST with example.
>GET:
- Can be bookmarked
Example:
- Server responds with a list of users matching the name and age
POST:
- Cannot be bookmarked
Q.8 What is Filter? List the applications of filter.
> Filter:
A filter is an object that can transform the request or response headers and content before and after
a servlet is executed.
- Methods:
- Attributes:
- Methods:
- Attributes:
These objects provide a way for servlets to interact with the client's request and generate a
response. The request object contains information about the client's request, such as parameters,
headers, and body data. The response object allows the servlet to set the status code, headers, and
body data for the response.
IV.Server Port No
V.Local Port No
>
Q.12 Write line(s) of code in JSP for following. I. Session read and write
II. URL rewriting sending and retrieving parameter(s)
>
Q.13 What is doFilter() method? What are its parameters? Give its use with proper
Example.
> The doFilter() method is a part of the Filter interface in Java Servlet programming. It is used to
perform filtering tasks, such as authentication, logging, or data compression, on requests and
responses.
Q.14 List out different types of servlet event and explain it.
> 1. ServletRequestEvent:
- Can be used to perform tasks such as logging, authentication, and resource allocation.
2. ServletRequestAttributeEvent:
- Provides access to the attribute name, value, and the request object.
3. ServletContextEvent:
- Can be used to perform tasks such as initializing or destroying resources, and registering or
unregistering servlets and filters.
4. ServletContextAttributeEvent:
5. HttpSessionEvent:
- Can be used to perform tasks such as session management, authentication, and resource
allocation.
6. HttpSessionAttributeEvent:
- Provides access to the attribute name, value, and the session object.
7. ServletContainerInitializerEvent:
- Can be used to perform tasks such as registering or unregistering servlets and filters, and
initializing or destroying resources.
Q.15 What is servlet filters? Give the necessary API for filters and explain their use.
>
Q.17 Give the characteristics of the HTTP protocol and explain the GET, HEADand PUT
methods of the HTTP protocol.
> Characteristics of HTTP Protocol:
2. Stateless: HTTP is a stateless protocol, meaning that each request is independent of the previous
requests.
3. Connectionless: HTTP is a connectionless protocol, meaning that a new connection is established
for each request.
HTTP Methods:
GET:
- Response: The server returns the requested resource in the response body.
HEAD:
- Purpose: Retrieve metadata about a resource without fetching the resource itself.
- Response: The server returns only the HTTP headers, without the response body.
PUT:
- Request Body: The request body contains the resource data to be created or updated.
- Idempotent: Yes, PUT is an idempotent method, meaning that making the same request multiple
times has the same effect as making it once.
These three methods are commonly used in HTTP communication, and understanding their purposes
and characteristics is essential for building web applications and services.
Model: It represents the business layer of application. It is an object to carry the data that can also
contain the logic to update controller if data is changed.
View: It represents the presentation layer of application. It is used to visualize the data that the
model contains.
Controller: It works on both the model and view. It is used to manage the flow of application, i.e.
data flow in the model object and to update the view whenever data is changed.
1. A client (browser) sends a request to the controller on the server side, for a page.
2. The controller then calls the model. It gathers the requested data.
3. Then the controller transfers the data retrieved to the view layer.
4. Now the result is sent back to the browser (client) by the view.
Model Layer
The Model in the MVC design pattern acts as a data layer for the application. It represents the
business logic for application and also the state of application. The model object fetch and store the
model state in the database. Using the model layer, rules are applied to the data that represents the
concepts of application.
View Layer
As the name depicts, view represents the visualization of data received from the model. The view
layer consists of output of application or user interface. It sends the requested data to the client, that
is fetched from model layer by controller.
Controller Layer
The controller layer gets the user requests from the view layer and processes them, with the
necessary validations. It acts as an interface between Model and View. The requests are then sent to
model for data processing. Once they are processed, the data is sent back to the controller and then
displayed on the view.
Q.19 What is Request Dispatcher? What is the difference between Request dispatcher’s
forward() and include() method?
> The RequestDispatcher interface provides the facility of dispatching the request to another
resource it may be html, servlet or jsp. This interface can also be used to include the content of
another resource also. It is one of the way of servlet collaboration.
As you see in the above figure, response of second servlet is sent to the client. Response of the first
servlet is not displayed to the user.
As you can see in the above figure, response of second servlet is included in the response of the first
servlet that is being sent to the client.