Getting Input from the User via HTML form - Java Servlet JSP

Java examples for Servlet JSP:Servlet

Description

Getting Input from the User via HTML form

<html>
               <head>
                 <title>Input Servlet</title>
               </head>
               <body>
                 <form action="/servlet/InputServlet" method="post">
                    Enter your name:&nbsp;
                    <input type="text" name="Name">
                    <br><br>
                    <input type="submit" value="Submit">
                 </form>
               </body>
            </html>

The InputServlet servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InputServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String name = request.getParameter("Name");

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Input Servlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>");
    out.println("Hello " + name);
    out.println("</h1>");
    out.println("</body>");
    out.println("</html>");
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    doGet(request, response);
  }
}

Related Tutorials