Wednesday 8 October 2014

Handling multiple fields from a form in a Servlet

This program gives a login html page to the user with multiple fields and directs the user to the Servlet on clicking the submit button. In the servlet the user is given a tabular form of the information that he filled in the html form.
Details.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
      <h1><marquee>Login Form</marquee></h1>
      <form action="LoginCls">
      <table align="center">
            <tr>
                  <td>Login</td>
                  <td><input type="text" name="username"></td>
            </tr>
            <tr>
                  <td>Passoword</td>
                  <td><input type="password" name="password"></td>
            </tr>
            <tr>
                  <td>FirstName</td>
                  <td><input type="text" name="firstname"></td>
            </tr>
            <tr>
                  <td>Last Name</td>
                  <td><input type="text" name="lastname"></td>
            </tr>
            <tr>
                  <td>Branch</td>
                  <td><input type="text" name="branch"></td>
            </tr>
            <tr>
                  <td>Sem</td>
                  <td><input type="text" name="sem"></td>
            </tr>
            <tr>
                  <td>Sex</td>
                  <td>
                        <input type="radio" name="Gender" value="Male">Male
                        <input type="radio" name="Gender" value="Female">Female
                  </td>
            </tr>
            <tr>
                  <td ><input type="submit" name="Submit"></td>
            </tr>
      </table>
           
      </form>
</body>
</html>

/**************************************************/
LoginCls.java
package LoginPkg;

import java.io.IOException ;
import java.io.PrintWriter;
import java.util.Enumeration;

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

import com.sun.jmx.snmp.Enumerated;

/**
 * Servlet implementation class LoginCls
 */
@WebServlet("/LoginCls")
public class LoginCls extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            PrintWriter pw=response.getWriter();
            Enumeration pNames=request.getParameterNames();
            String str;
            str=  "<html>"+
                        "<body>"+
                              "<h3><marquee>Login Form Details in table</marquee></h3>"+
                              "<table align=\"center\" border=\"5\">";
            while(pNames.hasMoreElements())
            {
                  String s=(String)pNames.nextElement();
                  String value=request.getParameter(s);
                  if(value.equals(""))
                        value="NIL";
                                    str+= "<tr>"+
                                                      "<td>"+s+"</td>"+
                                                      "<td>"+value+"</td>"+
                                                "</tr>";
            }
            str+=                   "</table>"+
                                          "</body>"+
                                    "</html>";
            pw.println(str);

      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}

No comments:

Post a Comment