Monday, 14 April 2014

Servlets : ReadAllParameters [Map]


Click here to watch in Youtube : https://www.youtube.com/watch?v=aKeoWB9O5t4

Click the below Image to Enlarge

ServletFormReadAllParam Project Dir Structure

ReadAllParamsServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

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

public class ReadAllParamsServlet extends HttpServlet
{

    private static final long serialVersionUID = 1L;

    public void init()
    {
        System.out
                .println("-----------------------------------------------------");
        System.out
                .println("init method has been called and servlet is initialized");
        System.out
                .println("-----------------------------------------------------");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        String title = "Reading All Form Parameters";
        String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
                + "transitional//en\">\n";
        out.println(docType + "<html>\n" + "<head><title>" + title
                + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"
                + "<h1 align=\"center\">" + title + "</h1>\n"
                + "<table width=\"100%\" border=\"1\" align=\"center\">\n"
                + "<tr bgcolor=\"#949494\">\n"
                + "<th>Param Name</th><th>Param Value(s)</th>\n" + "</tr>\n");

        Map<String, String[]> paramMap = request.getParameterMap();
        Set<String> paramNameSet = paramMap.keySet();

        for (String paramName : paramNameSet)
        {

            out.print("<tr><td>" + paramName + "</td>\n<td>");
            String[] paramValues = request.getParameterValues(paramName);
            // Read single valued data
            if (paramValues.length == 1)
            {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                {
                    out.println("<i>No Value</i>");
                }
                else
                {
                    out.println(paramValue);
                }
            }
            else
            {
                // Read multiple valued data
                out.println("<ul>");
                for (int i = 0; i < paramValues.length; i++)
                {
                    out.println("<li>" + paramValues[i]);
                }
                out.println("</ul>");
            }
        }
        out.println("</tr>\n</table>\n</body></html>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doGet(request, response);
    }

    public void destroy()
    {
        System.out
                .println("-----------------------------------------------------");
        System.out
                .println("destroy method has been called and servlet is destroyed");
        System.out
                .println("-----------------------------------------------------");
    }
}

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">
  <display-name>ServletForm GET</display-name>
  <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>
  <servlet>
    <servlet-name>readAllParams</servlet-name>
    <servlet-class>ReadAllParamsServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>readAllParams</servlet-name>
    <url-pattern>/readAllParams</url-pattern>
  </servlet-mapping>
</web-app>

index.html
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Order Pizza</title>
</head>
<body>
    <p>
    <h3>Order Pizza</H3>
    <p></p>
    <ul>
        <li><a href="pizza.html">Order Pizza</a></li>
    </ul>
</body>
</html>


pizza.html
<html>
<head>
<title>Retrieving All Parameters</title>
 </head>
<body>
<h4>Order your Pizza Here</h4>
<form action="readAllParams" method="GET">
<b>Name</b> <input type="text" name="name"><br><br>
<b>Select the Crust:</b>
<select name = "crust">
<option value="pan">Pan</option>
<option value="thin">Thin Crust</option>
<option value="deep">Deep Crust</option>
<option value="cheese">Cheese Burst</option>
</select> <br><br>
<b>Toppings: </b><br>
<input type="checkbox" name="toppings" value="peas">Peas<br>
<input type="checkbox" name="toppings" value="paneer">Paneer<br>
<input type="checkbox" name="toppings" value="redpeppers">Red Peppers<br>
<input type="checkbox" name="toppings" value="pineapple">Pineapple<br>
<input type="checkbox" name="toppings" value="onion">Onion<br>
<input type="checkbox" name="toppings" value="tomato">Tomato<br><br>
 
<b>Select 1 FREE Appetizer</b>
<input type="radio" name="appetizer" value="Garlic Bread">Garlic Bread
<input type="radio" name="appetizer" value="Cheese Garlic Bread">Cheese Garlic Bread
<input type="radio" name="appetizer" value="Veg Soup">Veg Soup
<input type="radio" name="appetizer" value="Veg Sandwich">Veg Sandwich<br><br>
 
<b>Address</b><br>
<textarea name="address" rows=3 cols=40></textarea><br><br>
<b>Credit Card:</b><br>
<input type="radio" name="cardType" value="Visa">Visa
<input type="radio" name="cardType" value="MasterCard">MasterCard
<input type="radio" name="cardType" value="Amex">American Express
<br><br>
<b>Credit Card Number:</b> <input type="password" name="cardNum">
<b>Repeat Credit Card Number: </b><input type="password" name="cardNum"><br><br>
<input type="submit" name="submit" value="Order Pizza">
</form>
</body>
</html>

Environment Used 

JDK version :1.6.0_30
Tomcat version : 7.0.50 

To Download ServletFormReadAllParam_Map_App Project Click the below link
https://sites.google.com/site/javaee4321/servlets/ServletFormReadAllParam_Map_App.zip?attredirects=0&d=1 

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • No comments:

    Post a Comment