Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Tuesday, February 20, 2024

SERVLET PROGRAM TO READ THE PARAMETERS by G Krishna

Dear Students,


If your Eclipse is not  Dynamic Web Project enabled then you can click here to know how to do this.


Click here to learn How to start a new Servlet project.


Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" id="WebApp_ID" version="6.0">

<display-name>Para</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.jsp</welcome-file>

<welcome-file>default.htm</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>ParameterServlet</servlet-name>

<servlet-class>ParameterServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ParameterServlet</servlet-name>

<url-pattern>/ParameterServlet</url-pattern>

</servlet-mapping>

</web-app>




ParameterForm.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Parameter Form</title>

<style>

body {

font-family: Arial, sans-serif;

background-color: #f4f4f4;

margin: 0;

padding: 0;

}

.container {

max-width: 400px;

margin: 50px auto;

padding: 20px;

background-color: #fff;

border-radius: 8px;

box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

}

h2 {

text-align: center;

margin-bottom: 20px;

}

form {

display: flex;

flex-direction: column;

}

label {

font-weight: bold;

margin-bottom: 5px;

}

input[type="text"] {

padding: 8px;

margin-bottom: 15px;

border-radius: 4px;

border: 1px solid #ccc;

}

input[type="submit"] {

padding: 10px 20px;

background-color: #007bff;

color: #fff;

border: none;

border-radius: 4px;

cursor: pointer;

transition: background-color 0.3s ease;

}

input[type="submit"]:hover {

background-color: #0056b3;

}

</style>

</head>

<body>

<div class="container">

<h2>Enter Your Details</h2>

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

<label for="firstName">First Name:</label>

<input type="text" id="firstName" name="firstName">

<label for="lastName">Last Name:</label>

<input type="text" id="lastName" name="lastName">

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

</form>

</div>

</body>

</html>


ParameterServlet.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;

import java.io.PrintWriter;


@WebServlet("/ParameterServlet")

public class ParameterServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


public ParameterServlet() {

super();

}


protected void dopost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

// Retrieve parameter values from the request

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

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


PrintWriter out = response.getWriter();

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Parameter Servlet Response</title>");

out.println("</head>");

out.println("<body>");

out.println("<h2>Post Method - Parameter Servlet Response</h2>");


// Print the first name and last name in the response

out.println("<p>First Name: " + firstName + "</p>");

out.println("<p>Last Name: " + lastName + "</p>");


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

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

}


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

doGet(request, response);

}

}


Output:







No comments:

Post a Comment