0% found this document useful (0 votes)
10 views7 pages

MOD4 Ajava Both Model Paper Answers Removed Removed

Uploaded by

muttu1757
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)
10 views7 pages

MOD4 Ajava Both Model Paper Answers Removed Removed

Uploaded by

muttu1757
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/ 7

Module 4

21CS642 Solved Model Question Paper-1

7.A] List and explain core classes and interfaces in the javax.servlet package.
Answer:
INTERFACES CLASSES

Servlet ServletInputStream

ServletContext ServletOutputStream

ServletConfig ServletException

ServletRequest UnavailableException

ServletResponse GenericServlet

Interface Summary

Servlet Defines methods that all servlets must implement.

ServletRequest Defines an object to provide client request information to a servlet.

ServletResponse Defines an object to assist a servlet in sending a response to the client.

A servlet configuration object used by a servlet container to pass information to a


ServletConfig
servlet during initialization.

Defines a set of methods that a servlet uses to communicate with its servlet
ServletContext container, for example, to get the MIME type of a file, dispatch requests, or write to a
log file.

Class Summary

GenericServlet Defines a generic, protocol-independent servlet.

Provides an input stream for reading binary data from a client request,
ServletInputStream
including an efficient readLine method for reading data one line at a time.

ServletOutputStream Provides an output stream for sending binary data to the client.

ServletException Defines a general exception a servlet can throw when it encounters difficulty.

Amruth Patil, Dept of CSE, DSATM


6. UnavailableException Class
• Purpose: Signals that a servlet is temporarily unavailable and cannot handle requests at the
moment.
• Key Methods:
• int getUnavailableSeconds(): Returns the number of seconds the servlet will be unavailable.
• String getMessage(): Returns a detailed message about the exception.
Summary
• Interfaces:
• Servlet: Base interface for all servlets.
• ServletRequest: Provides methods for request data.
• ServletResponse: Defines methods for sending responses.
• ServletConfig: Provides servlet configuration information.
• ServletContext: Provides context information and resource interaction.
• RequestDispatcher: Manages request forwarding and inclusion.
• Classes:
• GenericServlet: Convenience class for basic servlet functionality.
• HttpServlet: Handles HTTP-specific requests.
• ServletInputStream: Reads binary data from requests.
• ServletOutputStream: Sends binary data in responses.
• ServletException: Signals servlet errors.
• UnavailableException: Indicates temporary servlet unavailability.

7.B] Explain how to read servlet parameters with an example


Answer:
Note: if difficult to understand, Reading Form Parameters is enough
In a servlet, parameters are often sent from the client (e.g., via an HTML form) and can be
accessed using the HttpServletRequest object. Parameters can be accessed in a variety of ways,
depending on whether they are query parameters, form parameters, or parameters from a URL.

Here’s a breakdown of how to read servlet parameters:


1. Query Parameters
• Purpose: Parameters included in the URL after the ? (query string). Example
URL: http://example.com/servlet?param1=value1&param2=value2.
• Method to Use: getParameter(String name)
• Example Code:
import javax.servlet.*;

import javax.servlet.http.*;

import java.io.IOException;

public class QueryParamServlet extends HttpServlet {

@Override

Amruth Patil, Dept of CSE, DSATM


out.println("</body></html>");

Summary
1. Query Parameters:
• Accessed using req.getParameter("paramName").
• Sent as part of the URL after the ?.
2. Form Parameters:
• Accessed using req.getParameter("paramName").
• Sent via an HTML form with method="post" or method="get".
3. URL Path Parameters:
• Accessed using req.getPathInfo() or req.getServletPath().
• Included in the URL path, often used in RESTful services.

8.A] Explain how to handle HTTP request and response with an example
Answer:
In Java servlets, handling HTTP requests and responses involves overriding methods from
the HttpServlet class. The most common methods are doGet() and doPost(), which correspond to
HTTP GET and POST requests, respectively. Here’s a breakdown of how to handle these
requests and responses with examples.
Common HTTP Methods
1. GET Method
• Purpose: Retrieves data from the server.
• Usage: Often used for requesting data or resources.
• Characteristics: Parameters are sent in the URL.
• Example URL: http://example.com/servlet?param1=value1&param2=value2
2. POST Method
• Purpose: Submits data to be processed by the server.
• Usage: Often used for form submissions and data modifications.
• Characteristics: Parameters are sent in the body of the request, not in the URL.
Example: Handling HTTP Requests and Responses
1. HTML Form for GET and POST Requests
<!DOCTYPE html>

<html>

<head>

<title>Form Example</title>

</head>

<body>

<!-- Form to handle GET requests -->

Amruth Patil, Dept of CSE, DSATM


<h2>GET Request</h2>

<form method="get" action="getServlet">

Name: <input type="text" name="name">

<input type="submit" value="Submit">

</form>

<!-- Form to handle POST requests -->

<h2>POST Request</h2>

<form method="post" action="postServlet">

Name: <input type="text" name="name">

<input type="submit" value="Submit">

</form>

</body>

</html>

2. Handling GET Requests in a Servlet


import javax.servlet.*;

import javax.servlet.http.*;

import java.io.IOException;

import java.io.PrintWriter;

public class GetServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

// Set content type

response.setContentType("text/html");

// Get the PrintWriter to write the response

PrintWriter out = response.getWriter();

// Retrieve parameters from the request

String name = request.getParameter("name");

// Generate response

out.println("<html><body>");

out.println("<h2>GET Request</h2>");

out.println("<p>Hello, " + name + "!</p>");

out.println("</body></html>");

Amruth Patil, Dept of CSE, DSATM


// Close the PrintWriter

out.close();

3. Handling POST Requests in a Servlet


import javax.servlet.*;

import javax.servlet.http.*;

import java.io.IOException;

import java.io.PrintWriter;

public class PostServlet extends HttpServlet {

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

// Set content type

response.setContentType("text/html");

// Get the PrintWriter to write the response

PrintWriter out = response.getWriter();

// Retrieve parameters from the request

String name = request.getParameter("name");

// Generate response

out.println("<html><body>");

out.println("<h2>POST Request</h2>");

out.println("<p>Hello, " + name + "!</p>");

out.println("</body></html>");

// Close the PrintWriter

out.close();

Explanation
1. HTML Form:
• GET Form: Submits data using the GET method, appending parameters to the URL.
• POST Form: Submits data using the POST method, including parameters in the request body.
2. Servlets:

Amruth Patil, Dept of CSE, DSATM


• doGet() Method: Handles GET requests. Retrieves parameters from the URL and generates a
response.
• doPost() Method: Handles POST requests. Retrieves parameters from the request body and
generates a response.
Differences Between GET and POST Requests:
Feature GET Request POST Request

Data is included in the request


Data Data is visible in the URL query string. Suitable
body. Suitable for sensitive data
Visibility for non-sensitive data.
and larger amounts of data.

Limited by URL length restrictions (typically


Data Length No practical limits on data size.
around 2048 characters).

Idempotent; multiple identical GET requests Not necessarily idempotent; each


Idempotency
should have the same effect as a single request. request can have a different effect.

Requests can be cached by browsers and


Caching Requests are generally not cached.
servers.

8.B] Write a note on:


i) Session Tracking
ii) Cookies
Answer:
i) Session Tracking
• Session simply means a particular interval of time.
• Session Tracking is a way to maintain state (data) of an user. It is also known as session management in
servlet.
• Http protocol is a stateless so we need to maintain state using session tracking techniques.
• Each time user requests to the server, server treats the request as the new request.
• So we need to maintain the state of an user to recognize to particular user.
There are 2 techniques used in Session tracking:
• Cookies
• HttpSession

ii) Cookies
A cookie is a small piece of information that is persisted between the multiple client requests.
By default, each request is considered as a new request.
In cookies technique, we add cookie with response from the servlet. So cookie is stored in the cache of
the browser. After that if request is sent by the user, cookie is added with request by default. Thus, we
recognize the user as the old user.

Amruth Patil, Dept of CSE, DSATM


Advantage of Cookies
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
2. Only textual information can be set in Cookie object.
Note: Additional Expected Question below:

Example Demonstrating Usage of Cookies


Here is a complete example of how to use cookies to store and retrieve user information across different
servlets:
1. HTML Form (index.html)
<form method="post" action="MyServlet">

Name: <input type="text" name="user" /><br/>

Password: <input type="password" name="pass" /><br/>

<input type="submit" value="Submit" />

</form>
Amruth Patil, Dept of CSE, DSATM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy