0% found this document useful (0 votes)
2 views2 pages

Input From User

The document consists of three main components: an HTML form for user input, a Java servlet that processes the input and generates a personalized greeting, and a web.xml configuration file that maps the servlet. The HTML form collects a username and submits it to the HelloServlet via a POST request. The servlet responds with a greeting message that includes the entered username, and it can also handle GET requests by redirecting them to the POST method.

Uploaded by

sanskaruravane9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Input From User

The document consists of three main components: an HTML form for user input, a Java servlet that processes the input and generates a personalized greeting, and a web.xml configuration file that maps the servlet. The HTML form collects a username and submits it to the HelloServlet via a POST request. The servlet responds with a greeting message that includes the entered username, and it can also handle GET requests by redirecting them to the POST method.

Uploaded by

sanskaruravane9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

index.

html
<!DOCTYPE html>
<html>
<head>
<title>User Input</title>
</head>
<body>
<h2>Enter your username:</h2>
<form action="HelloServlet" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<input type="submit" value="Submit">
</form>
</body>
</html>

HelloServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get the username from the form input
String username = request.getParameter("username");
// Set content type of the response
response.setContentType("text/html");
// Get the output stream to send a response
PrintWriter out = response.getWriter();
// Display the response message
out.println("<html><body>");
out.println("<h2>Hello, " + username + "!</h2>");
out.println("</body></html>");
}
// doGet() method can be added to handle GET requests if needed
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}

web.xml
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

You might also like