Search This Blog

About Me

My photo
Blog Written By Dr. Digvijaysinh D Virpura

Monday, August 31, 2020

Create Servlet with doPost method.

 This blog will demonstrate with dopost method in Servlet    

Step 1: First create one html file and add following code in it.

 

<html>
<body>
<form action="HelloFormPost" method="POST">
First Name: <input name="first_name" type="text" />
<br />
Last Name: <input name="last_name" type="text" />
<input checked="checked" name="maths" type="checkbox" /> Maths
<input name="physics" type="checkbox" /> Physics
<input checked="checked" name="chemistry" type="checkbox" />
                                                Chemistry
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

Step 2: Now create one servlet file as  HelloFormPost

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloFormPost extends HttpServlet {

  // Method to handle GET method request.
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      PrintWriter out = response.getWriter();
      String title = "Using Post Method to Read Form Data";
      String docType =
      "\n";
      out.println(docType +"\n" + "First Name: "+request.getParameter("first_name") + "\n" + "Last Name: " + request.getParameter("last_name") +
       "\n\n\n\n"   +"<li><b>Maths Flag : </b>: "  + request.getParameter("maths") +
       "\n" +"<li><b>Physics Flag: </b>: "   + request.getParameter("physics") +
       "\n" +"<li><b>Chemistry Flag: </b>: "
                + request.getParameter("chemistry") + "\n" + "");
  }

 // Method to handle POST method request.
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }

}

 Step 3: Now go to html and run the file.

No comments:

Post a Comment