Monday, 28 July 2014

GetParameterMap ()

Map<String,String[]> getParameterMap()
Returns a Map of the parameters of this request.

Example : Prints all parameter names
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Registration</title>
    </head>
    <body>
        <form method="post" action="/servlet/Registration">
            <table id="registrationTable">
            <caption>Please fill the details to Register</caption>
                <tr>
                    <td>User Name:<sup>*</sup></td>
                    <td><input type="text" name="userName" required="required" /></td>
                </tr>
                <tr>
                    <td>Mail Id:<sup>*</sup></td>
                    <td><input type="text" name="mailID" required="required" /></td>
                </tr>
                <tr>
                    <td>Password:<sup>*</sup></td>
                    <td><input type="password" name="passwd" required="required" /></td>
                </tr>
                <tr>
                    <td>Confirm Password<sup>*</sup></td>
                    <td><input type="password" name="confPasswd" id="confPasswd" required="required" /></td>
                </tr>
                <tr>
                    <td>Hobbies<sup>*</sup></td>
                    <td>
                        <input type="checkbox" name="hobby">Cricket<br>
                        <input type="checkbox" name="hobby">Movies<br>
                        <input type="checkbox" name="hobby">FootBall<br>
                        <input type="checkbox" name="hobby">Chess<br>
                        <input type="checkbox" name="hobby">Driving<br>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="Register" /></td>
                    <td><input type="reset" value="Clear Data" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>Registration</servlet-name>
        <servlet-class>Registration</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Registration</servlet-name>
        <url-pattern>/Registration</url-pattern>
    </servlet-mapping>
</web-app>

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

public class Registration extends HttpServlet {

   @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       
       Map<String, String[]> param = req.getParameterMap();
       Set<String> keys;
       Iterator<String> iter;
       
       PrintWriter out = res.getWriter();
       res.setContentType("text/html");
       
       String str1 = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body><h1>";
                
       String str2 = "</h1> </body>" +
                    "</html>";
      
       StringBuilder str3 = new StringBuilder();
       keys = param.keySet();
       iter = keys.iterator();
       
       while(iter.hasNext()){
           String s1 = iter.next();
           str3.append(s1).append("<br />");
       }
       
       out.println(str1 + " "+str3.toString() +" " + str2);
   } 
}

Output






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment