MOD4 Ajava Both Model Paper Answers Removed Removed
MOD4 Ajava Both Model Paper Answers Removed Removed
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
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
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.
import javax.servlet.http.*;
import java.io.IOException;
@Override
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¶m2=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>
<h2>POST Request</h2>
</form>
</body>
</html>
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
@Override
response.setContentType("text/html");
// Generate response
out.println("<html><body>");
out.println("<h2>GET Request</h2>");
out.println("</body></html>");
out.close();
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
@Override
response.setContentType("text/html");
// Generate response
out.println("<html><body>");
out.println("<h2>POST Request</h2>");
out.println("</body></html>");
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:
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.
</form>
Amruth Patil, Dept of CSE, DSATM