How to read form data using JSP via POST Method?


Below is the main.jsp JSP program to handle the input given by web browser using the GET or the POST methods.

Infact there is no change in the above JSP because the only way of passing parameters is changed and no binary data is being passed to the JSP program. File handling related concepts will be explained in a separate chapter where we need to read the binary data stream.

<html>
   <head>
      <title>Using GET and POST Method to Read Form Data</title>
   </head>
   <body>
      <center>
         <h1>Using POST Method to Read Form Data</h1>
         <ul>
            <li><p><b>First Name:</b>
               <%= request.getParameter("first_name")%>
               </p></li>
            <li><p><b>Last Name:</b>
               <%= request.getParameter("last_name")%>
               </p></li>
         </ul>
      <center>
   </body>
</html>

Following is the content of the Hello.htm file −

<html>
   <body>
      <form action = "main.jsp" method = "POST">
         First Name: <input type = "text" name = "first_name">
         <br />
         Last Name: <input type = "text" name = "last_name" />
         <input type = "submit" value = "Submit" />
      </form>
   </body>
</html>

Let us now keep main.jsp and hello.htm in <Tomcat-installationdirectory>/webapps/ROOT directory. When you access http://localhost:8080/Hello.htm, you will receive the following output.

First Name:
Last Name:  

Try to enter the First and the Last Name and then click the submit button to see the result on your local machine where tomcat is running.

Based on the input provided, you will receive similar results as in the above examples.

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements