Monday 28 July 2014

getParameter (String name)

String getParameter(String name)
Returns the value associated with the parameter or null if the parameter not exist. For HTTP servlets, parameters are contained in the query string or posted form data.

For GET request parameters are contained in the query string. If the given parameter has more than one value like check box, you can use getParameterValues method.

If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues.

<%@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;

public class Registration extends HttpServlet {

   @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       
       String userName = req.getParameter("userName");
       String mailID = req.getParameter("mailID");
       
       PrintWriter out = res.getWriter();
       res.setContentType("text/html");
       String str = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> Please Check your details <br />User Name :" + userName +
                    "<br />Mail Id " + mailID +
                    "</h1> </body>" +
                    "</html>";
       out.println(str);
   } 

}


Output


<input type="text" name="userName" required="required" />

req.getParameter("userName")
statement returns the value associated with the request parameter name "userName".




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment