Name Input



Name Input


This example uses an HTML form to create a POST request containing the user’s name.

index.html contains an HTML form:

<!DOCTYPE html><html><head><title>Name Form</title></head><body><h1>What's your name?</h1><formaction="/post-name/hello"method="POST"><inputtype="text"name="name"value="Ada"><br><br><inputtype="submit"value="Submit"></form></body></html>

GreetingServlet.java handles the POST request by outputting the user’s name to the response:

packageio.happycoding.servlets;importjava.io.IOException;importjakarta.servlet.annotation.WebServlet;importjakarta.servlet.http.HttpServlet;importjakarta.servlet.http.HttpServletRequest;importjakarta.servlet.http.HttpServletResponse;@WebServlet("/hello")publicclassGreetingServletextendsHttpServlet{@OverridepublicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{Stringname=request.getParameter("name");response.getWriter().println("<h1>Hello "+name+"!</h1>");}}

name form

hello Ada



Post Requests Examples

Comments

Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!

close