0% found this document useful (0 votes)
5 views13 pages

222SOEIT047 Kumarharsh - Zip

The document outlines an experiment involving an HTTP Servlet that demonstrates the use of setIntHeader and setHeader methods to manage HTTP response headers. It includes five problems focusing on setting integer and string headers, testing the Content-Type header, implementing error handling, and measuring performance impacts. A JSP file serves as the user interface, allowing interaction with the servlet to execute various header-related functionalities.

Uploaded by

dsingh388
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)
5 views13 pages

222SOEIT047 Kumarharsh - Zip

The document outlines an experiment involving an HTTP Servlet that demonstrates the use of setIntHeader and setHeader methods to manage HTTP response headers. It includes five problems focusing on setting integer and string headers, testing the Content-Type header, implementing error handling, and measuring performance impacts. A JSP file serves as the user interface, allowing interaction with the servlet to execute various header-related functionalities.

Uploaded by

dsingh388
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/ 13

Experiment 3

Implement following problem using setIntHeader and setHeader.


Problem 1: Setting Integer Header with setIntHeader.

To set an integer value for an HTTP response header using setIntHeader.

1. Create an HTTP Servlet.

2. Within the doGet or doPost method:

a. Obtain the HttpServletResponse object


b. Use setIntHeader to set an integer value for a specific header (e.g., "Custom-
IntegerHeader").

c. Send the response.

Problem 2: Setting String Header with setHeader.

To set a string value for an HTTP response header using setHeader.

1. Create an HTTP Servlet.

2. Within the doGet or doPost method:

a. Obtain the HttpServletResponse object.


b. Use setHeader to set a string value for a specific header (e.g., "Custom-
StringHeader").

c. Send the response.

Problem 3: Testing Content-Type Header.


To set the Content-Type header using both setIntHeader and setHeader methods and observe
the differences.

1. Create an HTTP Servlet.

2. Within the doGet or doPost method:

a. Obtain the HttpServletResponse object


b. Use setIntHeader to attempt setting the Content-Type header with an integer value. c.
Use setHeader to set the Content-Type header with a string value like "text/plain" or
"application/json". d. Compare the results in the response headers using browser
developer tools or an HTTP client (like cURL or Postman).
Problem 4: Implement Error Handling and Exception Scenarios.

Handle potential exceptions or errors while using setIntHeader and setHeader.

1. Create an HTTP Servlet.

2. Introduce scenarios such as passing invalid input or incorrect header names/values to


setIntHeader and setHeader.

3. Implement try-catch blocks to handle potential exceptions that may arise due to
incorrect usage.

4. Log or display appropriate error messages if exceptions occur.

Problem 5: Impact on Response Size and Performance


Measure and compare the impact on response size and performance when setting headers using
setIntHeader and setHeader.

1. Create an HTTP Servlet that generates a sizable response (JSP page, HTML content).

2. Measure the response size and performance (time taken to generate and send the
response) with headers set using setIntHeader.

3. Repeat the measurement with headers set using setHeader.

4. Analyze the differences in response size and performance between the two approaches.

In this experiment, we implemented a JSP and a Servlet to demonstrate the handling of


advanced HTTP headers and error scenarios. The JSP file serves as the front-end interface,
allowing users to interact with different functionalities through buttons. Each button
corresponds to a specific problem statement:

1. Setting Integer Header: Demonstrates the usage of setIntHeader to set custom integer
headers.

2. Setting String Header: Uses setHeader to define custom string headers.

3. Content-Type Header Test: Tests both correct and incorrect usage of the Content-Type
header.

4. Error Handling: Illustrates error detection and handling by triggering exceptions with
invalid headers.
5. Performance Testing: Measures the time taken to set multiple headers for performance
analysis.

JSP File (index.jsp)


<!DOCTYPE html>

<html>

<head>

<title>Experiment 3</title>

<style> /* Global styles */ body { font-

family: Arial, sans-serif; margin: 0; padding: 0;

background: linear-gradient(135deg, #1e3c72, #2a5298);

color: #fff;

display: flex; justify-

content: center; align-

items: center; height:

100vh;

h1 { font-size: 2.5rem;

text-align: center; margin-

bottom: 1rem; animation: fadeIn

1s ease-in-out;

} p{ font-size: 1.2rem;

text-align: center; margin-bottom:

2rem; animation: fadeIn 1.2s ease-

in-out;

/* Form and button styles */

form { display: flex;

flex-direction: column;

align-items: center;
}

button { background-

color: #4CAF50;

color: white; border: none; border-radius: 25px; padding:

15px 30px; font-size: 1rem; margin: 10px 0; cursor: pointer;

box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2); transition: transform 0.3s ease,

background-color 0.3s ease, box-shadow 0.3s ease;

button:hover { transform: scale(1.1);

background-color: #45a049; box-shadow:

0px 6px 8px rgba(0, 0, 0, 0.3);

button:active { transform: scale(0.95);

box-shadow: 0px 3px 5px rgba(0, 0, 0, 0.4);

/* Animations */

@keyframes fadeIn {

from { opacity: 0;

transform: translateY(-20px);

} to {

opacity: 1; transform:

translateY(0);

/* Footer */

footer { position:
absolute; bottom:

10px; font-size:

0.9rem; color:

#ddd;

</style>

</head>

<body>

<div>

<h1>Experiment 3</h1>

<p>Click on a button to see the solution for each problem statement:</p>

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

<button type="submit" name="action" value="setIntegerHeader">Problem 1: Set Integer Header</button>

<button type="submit" name="action" value="setStringHeader">Problem 2: Set String Header</button>

<button type="submit" name="action" value="testContentType">Problem 3: Test Content-Type


Header</button>

<button type="submit" name="action" value="handleError">Problem 4: Error Handling</button>

<button type="submit" name="action" value="testPerformance">Problem 5: Test Performance</button>

</form>

</div>

</body>

</html>

Servlet File (MainServlet.java)


import jakarta.servlet.ServletException; import

jakarta.servlet.annotation.WebServlet; import

jakarta.servlet.http.HttpServlet; import

jakarta.servlet.http.HttpServletRequest; import

jakarta.servlet.http.HttpServletResponse; import

java.io.IOException;
@WebServlet("/MainServlet")

public class MainServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public MainServlet() {

super();

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,


IOException {

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

response.setContentType("text/html");

// Common advanced CSS and animations for all responses

String style = """ <style> body {

font-family: 'Roboto', sans-serif; background: linear-

gradient(135deg, #1f4037, #99f2c8);

margin: 0;

padding: 0;

display: flex; flex-

direction: column;

justify-content: center;

align-items: center;

height: 100vh;

color: white;

} h1 { font-size: 3em;

margin-top: 20px; text-shadow: 2px 2px

5px rgba(0, 0, 0, 0.5);

animation: textGlow 1.5s infinite alternate;


} p{ font-size:

1.5em; background: rgba(255, 255, 255,

0.1); padding: 15px; border-

radius: 10px; box-shadow: 0 4px 6px

rgba(0, 0, 0, 0.2); margin: 20px;

animation: fadeIn 1s ease-in-out;

} button { font-size: 1em;

padding: 12px 30px; margin: 10px;

background: linear-gradient(90deg, #ff7eb3, #ff758c);

border: none; border-radius: 25px; color:

white; cursor: pointer; box-shadow: 0px

4px 10px rgba(255, 117, 140, 0.5); transition: all 0.3s

ease-in-out;

animation: buttonPulse 2s infinite;

} button:hover { transform:

scale(1.1); background: linear-gradient(90deg,

#ff758c, #ff7eb3); box-shadow: 0px 6px 15px

rgba(255, 117, 140, 0.7);

.footer { margin-top:

30px; font-size: 1em;

color: rgba(255, 255, 255, 0.8);

@keyframes fadeIn { from { opacity:

0; transform: translateY(20px); } to { opacity: 1;

transform: translateY(0); }

@keyframes textGlow { from { text-shadow: 2px

2px 10px rgba(255, 255, 255, 0.4); } to { text-shadow: 2px

2px 20px rgba(255, 255, 255, 0.8); }


}

@keyframes buttonPulse {

0% { transform: scale(1); }

50% { transform: scale(1.05); }

100% { transform: scale(1); }

</style>

""";

StringBuilder htmlResponse = new StringBuilder("<html><head>" + style + "</head><body>");

if ("setIntegerHeader".equals(action))

// Problem 1: Set Integer Header response.setIntHeader("Custom-Integer-

Header", 1234567890); htmlResponse.append("<h1>Problem 1: Integer

Header</h1>");

htmlResponse.append("<p>Integer header <strong>'Custom-Integer-Header'</strong> has been set to


<strong>1234567890</strong>.</p>");

else if ("setStringHeader".equals(action))

// Problem 2: Set String Header response.setHeader("Custom-String-

Header", "HelloDuniya!"); htmlResponse.append("<h1>Problem 2: String

Header</h1>");

htmlResponse.append("<p>String header <strong>'Custom-String-Header'</strong> has been set to


<strong>'HelloDuniya!'</strong>.</p>");

else if ("testContentType".equals(action))

{
// Problem 3: Test Content-Type Header

response.setIntHeader("Content-Type", 12345); // Incorrect usage

response.setHeader("Content-Type", "text/html"); // Correct usage

htmlResponse.append("<h1>Problem 3: Content-Type Header</h1>");

htmlResponse.append("<p>Header <strong>'Content-Type'</strong> tested with incorrect and correct


usage. Check browser developer tools for details.</p>");

else if ("handleError".equals(action))

// Problem 4: Error Handling

try

response.setHeader(null, "InvalidValue");

catch (IllegalArgumentException e)

response.setContentType("text/html");

response.sendError(HttpServletResponse.SC_BAD_REQUEST, "<h1>Problem 4: Error


Handling</h1><p>Invalid header name or value detected.</p>");

htmlResponse.append("<h1>Problem 4: Error Handling</h1>");

htmlResponse.append("<p>Error handling completed successfully.</p>");

else if ("testPerformance".equals(action))

// Problem 5: Performance Testing long startTime =

System.currentTimeMillis(); response.setHeader("Custom-Header",

"Value1"); response.setIntHeader("Custom-Integer-Header", 98765);


long endTime = System.currentTimeMillis();

htmlResponse.append("<h1>Problem 5: Performance Testing</h1>");

htmlResponse.append("<p>Headers set successfully. Execution time: <strong>" + (endTime - startTime) +


"ms</strong>.</p>");

else

// Default case for invalid action htmlResponse.append("<h1>Error</h1>");

htmlResponse.append("<p>Invalid action! Please select a valid problem statement.</p>");

// Add footer

htmlResponse.append("<div class='footer'>Experiment with advanced HTTP headers using the buttons

provided.</div>"); htmlResponse.append("</body></html>");

response.getWriter().println(htmlResponse.toString());

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,


IOException { doGet(request, response);

}
When I tap “Set Integer Header” button it will redirect to http://localhost:8080/Experiment_3/MainServlet?action=setIntegerHeader and show this:
When I tap “Set Integer Header” button it will redirect to http://localhost:8080/Experiment_3/MainServlet?action=setStringHeader and show this:

When I tap “Set Integer Header” button it will redirect to http://localhost:8080/Experiment_3/MainServlet?action=testContentType and show this:
When I tap “Set Integer Header” button it will redirect to http://localhost:8080/Experiment_3/MainServlet?action=handleError and show this:

When I tap “Test Preformance” button it will redirect to http://localhost:8080/Experiment_3/MainServlet?action=testPerformance and show this:

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