Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for Enumeration form values
Previous Home Next

In this servlet program, Enumerate all value from HTML Form and display it on browser.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <servlet>
  <servlet-name>enumerationServlet</servlet-name>
  <servlet-class>r4r.servlet.enumerationServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>enumerationServlet</servlet-name>
  <url-pattern>/enumerationServlet</url-pattern>
 </servlet-mapping>
 <session-config>
   <session-timeout>
   30
   </session-timeout>
  </session-config>
</web-app>

Index.jsp

<%-- 
  Document   : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
 <html>
  <head>
   <meta http-equiv="Content-Type"
          content="text/html; charset=UTF-8">
   <title>r4r.co.in-servlet</title> 
  </head>
  <body>
   <form>
   <h1>Access Servlet by servlet's Name!</h1>
   <a href="enumerationServlet">enumerationServlet</a>
   </form>
  </body>
</html>

Servlet Program

/*
 * Save as a enumerationServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author R4R
 */
public class enumerationServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
 HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet enumerationServlet at "
       + request.getContextPath() + "</h1>");
out.println
 ("<form name=\"form\" action=\"enumerationServlet\" method=\"POST\">");
out.println("<table cellspacing=\"2\" cellpadding=\"2\">");
out.println("<tbody>");
out.println("<tr><td>Firstname</td>");
out.println
 (" <td> <input type=\"text\" name=\"fname\" value=\
        "\" size=\"20\" maxlength=\"10\" title=\"firstname\"/></td></tr>");
out.println("<tr> <td>Lastname</td>");
out.println
	("<td> <input type=\"text\" name=\"lname\" value=\
         "\" size=\"20\" maxlength=\"10\" title=\"lastname\"/></td> </tr>");
out.println("<tr><td>Age: </td>");
out.println
	("<td><input type=\"text\" name=\"age\" value=\
            "\" size=\"20\" maxlength=\"2\" title=\"Age\"/></td> </tr>");
out.println("<tr><td>Gender: </td>");
out.println("<td><input type=\"radio\" name=\"gender\
              " value=\"male\" title=\"Male\" /> Male");
out.println(" <input type=\"radio\" name=\"gender\" value=\
               "male\" title=\"Female\" /> Female </td> </tr>");
out.println("<tr> <td>Number: </td>");
out.println(" <td><input type=\"text\" name=\"number\" value=\
           "\" size=\"20\" maxlength=\"10\" title=\"number\" /> </td></tr>");
out.println("<tr> <td>Grade: </td>");
out.println(" <td><select name=\"grade\"> title=\"Grade\"");
out.println("<option> Low</option>");
out.println("<option> Medium </option>");
out.println("<option> High </option>");
out.println("</select> </td> </tr>");
out.println(" <tr> <td> <input type=\"submit\" value=\
                       "Submit Detail\" title=\"Submit Button\" /> </td>");
out.println("<td><input type=\"reset\" value=\"Reset Detail\
                  " title=\"Reset Button\" /> </td> </tr>");
out.println(" </tbody>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// Get all Parameter name value from Form
Enumeration enumeration = request.getParameterNames();
HttpSession session = request.getSession();
//Store Parameter name into session 
        

session.setAttribute("FormDetail", enumeration);
// Receive Attribute name form session
Enumeration enumeration1 = (Enumeration) session.getAttribute("FormDetail");
while (enumeration1.hasMoreElements()) {
String string = (String) enumeration1.nextElement();
// Display values on browser
out.println(string + " : " + request.getParameter(string) + "<BR>");
}
out.close(); //close text-output stream
}

@Override
public String getServletInfo() {
return "r4r.co.in-enumerationServlet";
}
}
Output of program
Previous Home Next