Submit Form to Servlet


index.html               (HTML File)


<!DOCTYPE html>

<html>

    <head>

        <title>Format</title>

        <style>

             .container{

                      

                      width:30%;

                      border:1px solid black;          

                      margin-top:10%;

                      margin-left:35%;

                      padding:20px;

                      Font-size:20px;           

             }     

             

             #myform table tr td input {

                 font-size:15px;

             }

        </style>

    </head>

         <body>

             <div class = "container">

             <h1>My Form</h1>

             <form id = "myform" action="RegisterServlet" method="post">

             <table>

                  <tr>

                    <td>Enter your name:</td>

                      <td><input type="text" name ="user name" placeholder="Enter your name"></td>

                 </tr>

 

                  <tr>

                    <td>Enter your password:</td>

                      <td><input type = "password" name ="pass" placeholder = "Enter your Password"></td>

                  </tr>

                     <tr>

                     <td>Enter your Email:</td>

                       <td><input type = "email" name = "user email" placeholder = "Enter your Email"></td>

                  </tr>

  

                  <tr>

                    <td>Select Gender:</td>

                      <td><input type = "radio" name = "user gender" value="male">Male <input type ="radio" name = "user gender" value = "Female">Female</td>

                  </tr>

                  <tr>

                    <td>Select your Course:</td>

                      <td>

                        <Select name="user Course">

                                                <option value = "Select Course">'Select Course'</option>

                                                <option value = "Java">Java</option>

                                                <option value = "Python">Python</option>

                                                <option value = "php">php</option>

                                                <option value = ".Net">.Net</option>

                                                <option value = "Hadoop">Hadoop</option>

                                                <option value = "Java">C++</option>

                       </Select>

                     </td>

                 </tr>

                 <tr>

                 <td style ="text-align:right;"><input type = "checkbox" value="checked" name = "condition"></td>

                 <td>Agree all Terms and Condition</td>

                 </tr>

                 <tr>

                 <td> </td>

                 <td>

                   <button type="submit">Register</button>

                   <button type="reset">Reset</button>

                 </td>

             </table> 

             </form>

             </div>

        </body>

</html>


RegisterServlet.java        (java File)


package com.ali.armaan;


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 RegisterServlet extends HttpServlet {

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

resp.setContentType("text/html");

PrintWriter p = resp.getWriter();

p.println("<h2>Welcome to register Servlet</h2>");

String name = req.getParameter("user name");

String pass = req.getParameter("pass");

String email = req.getParameter("user email");

String Gender = req.getParameter("user gender");

String course = req.getParameter("user Course");

String cond = req.getParameter("condition");

     if (cond !=null) {

     

   if(cond.equals("checked")) {

p.println("<h3> Name : "+name+" </h3>");

p.println("<h3> password : "+pass+" </h3>");

p.println("<h3> Email : "+email+" </h3>");

p.println("<h3> Gender : "+Gender+" </h3>");

p.println("<h3> Course : "+course+" </h3>");

}else {

p.println("<h2>you have not accepted Terms and Condition</h2>");

}

     else {

p.println("<h2>you have not accepted Terms and Condition</h2>");

}

}

}


web.xml        (mapping file )


<?xml version="1.0" encoding="UTF-8"?>


  <web-app version="4.0" 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_4_0.xsd"> 

                

        

    <servlet>

    <servlet-name>register</servlet-name>

    <servlet-class>com.ali.armaan.RegisterServlet</servlet-class>

    </servlet>

    

    <servlet-mapping>

    <servlet-name>register</servlet-name>

    <url-pattern>/RegisterServlet</url-pattern>

    </servlet-mapping>

      

  </web-app>

    






Comments

Popular posts from this blog

JAVA is both compiled and an interpreted language, JDM.