Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

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

In this servlet program, Iterate all values 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>IteratorServlet</servlet-name>
  <servlet-class>r4r.servlet.IteratorServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>IteratorServlet</servlet-name>
  <url-pattern>/IteratorServlet</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="IteratorServlet">IteratorServlet</a>
   </form>
  </body>
</html>

Servlet Program

/*
 * Save as a IteratorServlet.java
 */
package r4r.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author R4R
 */
public class IteratorServlet 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=\
             "IteratorServlet\" 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 from Form
Iterator iterator= (Iterator) request.getParameterNames();
while (iterator.hasNext()) {
String object = (String) iterator.next();
// Display values on browser
out.println(object + " : " + request.getParameter(object) + "<BR>");
}
out.close(); // close text-output stream
}

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