How to read form data using JSP via POST Method?

JSPJava 8Object Oriented ProgrammingProgramming

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.

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

Following is the content of the Hello.htm file −

      <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" />

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.

Published on 20-Mar-2019 09:46:46