0% found this document useful (0 votes)
8 views5 pages

KripaAcharya AJP LAB3

This document outlines a lab project focused on developing a Java servlet for retrieving and displaying user-submitted data from an HTML form. It covers the theory behind servlets, their advantages and disadvantages, and provides code examples for a servlet and an HTML form. The lab aims to give students practical experience with HTTP requests, dynamic content generation, and the servlet lifecycle in web development.
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)
8 views5 pages

KripaAcharya AJP LAB3

This document outlines a lab project focused on developing a Java servlet for retrieving and displaying user-submitted data from an HTML form. It covers the theory behind servlets, their advantages and disadvantages, and provides code examples for a servlet and an HTML form. The lab aims to give students practical experience with HTTP requests, dynamic content generation, and the servlet lifecycle in web development.
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

LAB 3: DEVELOP A SERVLET FOR FORM DATA

RETRIEVAL

THEORY
In Java web development, servlets play a crucial role in handling server-side logic, especially
when dealing with client input from web forms. This lab project focuses on creating a basic
Java servlet that retrieves and displays user-submitted data via an HTML form. Servlets are
Java classes that respond to HTTP requests within a web container like Apache Tomcat. In this
task, a simple HTML form collects a username and password and sends them to the server
using the POST method. The servlet processes the request, extracts the data using the
HttpServletRequest object, and responds with a dynamically generated HTML page
displaying the received input. This foundational exercise demonstrates how Java handles form
submission, request-response cycles, and dynamic content generation in a web application
environment.

OBJECTIVES
 Understand the structure and lifecycle of a Java servlet.
 Design an HTML form with proper form fields and submission methods.
 Learn how to retrieve form data using HttpServletRequest methods.
 Use PrintWriter to generate dynamic HTML responses.
 Gain experience deploying servlets in a web container (e.g., Apache Tomcat).
 Test and validate servlet functionality via a web browser.

ADVANTAGES
 Platform Independent:
o Java servlets run on any server supporting the Servlet API.
 Efficient Processing:
o Servlets handle multiple requests efficiently with multi-threading.
 Dynamic content generation:
o Allows creation of dynamic web pages based on user input.
 Integration with Java ecosystem:
o Easily integrates with JDBC, JSP, and other Java technologies.
 Better performance over CGI:
o Unlike CGI scripts, servlets are managed as a single process, reducing overhead.

DISADVANTAGES
 Manual HTML generation:
o Using PrintWriter for HTML output can become tedious and error-prone
for complex pages.
 Steeper learning curve:
o Beginners may find servlet setup and deployment slightly complex.
 Limited view logic:
o Servlets are not ideal for UI rendering; JSP or frameworks are better suited.
 Verbose Code:
o Basic servlet are applications often require a lot of boilerplate code.
 Tight coupling of logic and presentation:
o Mixing business logic with presentation in servlets can lead to poor code
maintainability.

CODE
[Link]
package [Link].lab3;

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {

String user = [Link]("username");


String pass = [Link]("password");

[Link]("text/html");

PrintWriter out = [Link]();

[Link]("<!DOCTYPE html><html><head><title>Servlet
Response</title></head><body>");
[Link]("<h1>Data Received Successfully!</h1>");
[Link]("<p>Thank you for submitting your
information.</p>");
[Link]("<hr>");
[Link]("<p><b>Entered Username:</b> " + user +
"</p>");
[Link]("<p><b>Entered Password:</b> " + pass +
"</p>");
[Link]("<br><a href='[Link]'>Go Back to
Form</a>");
[Link]("</body></html>");

// Close the writer


[Link]();
}
}

[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Form</title>
</head>
<body>

<div class="container">
<h2>Lab 3: Form Data Retrieval</h2>
<form action="MyServlet" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"
placeholder="Enter your username" required><br><br>

<label for="password">Password:</label><br>
<input type="password" id="password" name="password"
placeholder="Enter your password" required><br><br>

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


</form>
</div>

</body>
</html>
OUTPUT

CONCLUSION
This lab exercise provides a practical introduction to Java servlets and their role in web
development. By designing a simple HTML form and processing the submitted data using a
servlet, students gain hands-on experience with handling HTTP POST requests, extracting
parameters, and generating dynamic responses. It reinforces the core concepts of client-server
communication and servlet lifecycle, forming a foundation for more advanced web
applications. Although servlets alone may not be ideal for large-scale UI development,
understanding their mechanics is essential for grasping the broader Java EE ecosystem and for
building scalable, server-side logic in modern web systems.

You might also like