Wednesday, 16 July 2014

doPost (HttpServletRequest req, HttpServletResponse resp)

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

Called by the server (via the service method) to allow a servlet to handle a POST request. The HTTP POST method allows the client to send data of unlimited length to the Web server a single time and is useful when posting information such as credit card numbers, passwords and some sensitive information.

If the HTTP POST request is incorrectly formatted, doPost returns an HTTP "Bad Request" message.

<!DOCTYPE html>
<html>
    <head>
        <title>User Registration</title>
    </head>
    <body>
        <noscript>Java Script functionality disabled, Please enable it</noscript>
        <form id="formRegistration" action="/servlet/Register" method="post">
            <table id="registrationTable">
                <caption>Please fill the details to Register</caption>
                <tr>
                    <td>Mail Id:<span class="required1"><sup>*</sup></span></td>
                    <td><input type="text" name="mailID" id="mailID" required="required" /></td>
                </tr>
                <tr>
                    <td>Password:<span class="required1"><sup>*</sup></span></td>
                    <td><input type="password" name="passwd" id="passwd" required="required" /></td>
                </tr>
                <tr>
                    <td>Confirm Password<span class="required1"><sup>*</sup></span></td>
                    <td><input type="password" name="confPasswd" id="confPasswd" required="required" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Register" id="register" /></td>
                    <td><input type="reset" value="Clear Data" /></td>             
                </tr>
            </table>
        </form>
    </body>
</html>

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 Register extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       
       String mailId = req.getParameter("mailID");
       PrintWriter out = res.getWriter();
       res.setContentType("text/html");
       String str = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> User name :" + mailId + " Successfully stored in database" +
                    "</h1> </body>" +
                    "</html>";
       out.println(str);
   } 
}

<?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>Register</servlet-name>
        <servlet-class>Register</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Register</servlet-name>
        <url-pattern>/Register</url-pattern>
    </servlet-mapping>
</web-app>

Output







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment