Servlet form example

This servlet form example shows you how to create HTML or JSP form and send form data to the servlet. In this tutorial I have followed servlet 3 annotations based, so please make sure you have servlet 3.1 jar.

Environment used

JDK 8 or greater version

Latest Eclipse EE for the web developer

Servlet 3. 1 jar or greater version

Tomcat 8.5 or greater version

Creating the form in JSP or HTML

This file contains the form with different input types for your better understanding

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

   Welcome to servlet 3 Form example
<br>
<br>
<form action="MyForm">
      Email : <input type="text" name="email"> <br> 
      User Name: <input type="text" name="uname"> <br> 
      Password: <input type="password" name="pass"> <br> 
      Gender : <input type="radio" name="gender" value="male" checked>
      Male <input type="radio" name="gender" value="female">
      Female <input type="radio" name="gender" value="other"> <br>
      Course: <input type="checkbox" name="course" value="Java">
      Java <input type="checkbox" name="course" value="Dot Net"> Dot
      Net <input type="checkbox" name="course" value="PHP"> PHP <br>
<input type="submit" value="Submit"> <input type="reset">
</form>

</body>
</html>

Now Let’s see how to send form data to the servlet

Once the form is filled and user click on the submit button, a HTTP request will be sent to our tomcat container

Tomcat container will map your action with your corresponding servlet controller and getParameter function form HttpServletRequest will retrieve field data.

Text box name should exactly match with the getParameter argument.

String em = request.getParameter("email");

Look at the below servlet controller

FormController

package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/MyForm")
public class FormController extends HttpServlet
{
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
   {

      String em = request.getParameter("email");
      String un = request.getParameter("uname");
      String pa = request.getParameter("pass");
      String ge = request.getParameter("gender");
      String[] co = request.getParameterValues("course");

      response.setContentType("text/html");
      PrintWriter pw=response.getWriter();
      pw.write("<h2> Following data received sucessfully.. <h2> <br>");
      pw.write("<h3> Email: "+ em +" </h3>");
      pw.write("<h3> User name: "+ un +" </h3>");
      pw.write("<h3> Password: "+ pa +" </h3>");
      pw.write("<h3> Gender: "+ ge +" </h3>");
      pw.write("<h3> Course:  ");
      for(String c:co)
      {
         pw.write(  c+"   ");
      }
      pw.write("</h3>");
   }
}

 

Output screenshot

 

Download

Servlet form data zip

Servlet form data war

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *