Check eligibility of voter - Java Program

Q. Write a Servlet program that accepts the age and name and displays if the user is eligible for voting or not.     

Answer:

In this example we will check if the user is eligible for voting or not. If the age is greater than 17, then user is eligible to vote. There is one html page which takes name and age from the user.

index.html

<!doctype html>
<html lang="en">
     <head>
          <meta charset="UTF-8">
          <title>VoterApp</title>
     </head>
     <body>
          <form action= "vturl" method="get">
               <fieldset style="width:20%; background-color:#80ffcc">
                    <table>
                         <tr><td>Name</td><td><input type="text" name="name"></td></tr>
                         <tr><td>Age</td><td><input type="text" name="age"></td></tr>
                         <tr><td></td><td><input type = "submit" value="check voting eligibility"></td></tr>
                    </table>
               </fieldset>
          </form>
     </body>
</html>


VoterSrv.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class VoterSrv extends HttpServlet
{
     public void service(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException
     {
          //set response content type
          res.setContentType("text/html");
          //get printWrite obj
          PrintWriter pw = res.getWriter();
          //read form data from page as request parameter
          String name = req.getParameter("name");
          int age = Integer.parseInt(req.getParameter("age"));
          if (age>=18)
          {
               pw.println("<font color='green' size='4'>"+name+" you are eligible to vote</font>");
          }
          else
               pw.println("<font color='red' size='4'>"+name+" you are not eligible to vote</font>");
          //add hyperlink to dynamic page
          pw.println("<br><br><a href= 'index.html'>Home</a>");
          //close the stream
          pw.close();
     }
}


web.xml

<web-app>
     <servlet>
          <servlet-name>abc</servlet-name>
          <servlet-class>VoterSrv</servlet-class>
     </servlet>
     <servlet-mapping>
          <servlet-name>abc</servlet-name>
          <url-pattern>/vturl</url-pattern>
     </servlet-mapping>
</web-app>


Output:

voter check

voter check


voter check

voter check