POST METHOD

In this example we are going to show how we can make use of the POST in the JSP. After going through this example you will better understand the concept of the POST. Post is mainly used to send the data to the server and to retrieve the data from the serv

POST METHOD

POST METHOD

     

In this example we are going to show how we can make use of the POST in the JSP. After going through this example you will better understand the concept of the POST. Post is mainly used to send the data to the server and to retrieve the data from the server. In POST the data will not get appended in the URL but sent across with the body of the content. 

Firstly in this example we are going to make one jsp form which will some textfields and a submit button. The user will fill up the form and the request will be submitted to the controller which will perform rest of the task and gives back the response. 

This form contains some textfield like name, password, one radio button that is of sex, one checkbox, one option button and a submit button. The user has to fill up the form and as soon as the user fills the form and clicks a submit button the request goes to the controller. The path of the controller is given in the form attribute action and the method which will be used to send the data to server is post which is defined in the form attribute method.

As soon as the controller gets the request it matches all the parameters we have passed in the form and the result is displayed to the user.

Code of the indexPostForm.html.txt file:

<html>
<head><title>JSP Form Post Example</title></head>
<body>
<center><table border="2">
<tr>
<td>JSP Form Post Example</td>
</tr>
<td>
</center></table>
<center><table border="2">
<form action="nextPageToPost.jsp" action="post">
<center><table border="2">
<tr>
<td align="center"colspan="2"><h4>
Enter data for post to next page <h4></td>
</tr>
<tr>
<td>Enter your name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Enter your password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><b>Sex</b></td>
<td><input type="radio" name="sex" value="Male">Male</td>
<td><input type="radio" name="sex" value="Female">Female</td>
</tr>
<tr>
<td>Are you regular visitor of RoseIndia.net</td>
<td><input type="checkbox" name="check"></td>
</tr>
<tr>
<td>Nationality</td>
<td><select name="nationality" size="1">
<option>India</option>
<option>USA</option>
<option>UK</option>
<option>SRI LANKA</option>
</td>
</tr>
<tr>
<td>Description</td>
</tr>
<tr>
<td><textarea rows="2" name="description" cols="20">
</textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
<td><input type="reset" name="reset" value="reset"></td>
</tr>
</form>
</table>	
</center>
</body>
</html>

Code of the nextPageToPost.jsp file:

<html>
<head><title>JSP Form Post Example</title></head>

<body>
<center><table border="2">
<tr>
<td><h3>Your message has been posted by using Post Method</h3></td> 
</tr>
</center></table>
<center><table border="2" >
<tr>
<td><h4>Name:</h4></td><td><%=request.getParameter("name") %></td> 
</tr>
<tr>
<td><h4>Password:</h4></td><td><%=request.getParameter("name") %></td> 
</tr>
<tr>
<td><h4>Sex:</h4></td><td><%=request.getParameter("sex")%></td> 
</tr>
<tr>
<td><h4>Are you regular <br>visitor of RoseIndia.ne</h4></td><td>
<% if(request.getParameter("check").equals("on"))out.println("Yes"); %></td> 
</tr>
<tr>
<td><h4>Nationality</h4></td><td><%=request.getParameter("nationality") %>
</td> 
</tr>
<tr>
<td><h4>Description</h4></td><td><%=request.getParameter("description") %>
</td> 
</tr>

</center></table>

</body>
</html>

The output of the program is given below:

Download nextPageToPost.jsp file.

Download indexPostForm..html file.