Home Jsp POST METHOD
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

POST METHOD

Advertisement
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

     

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.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Ask Questions?    Discuss: POST METHOD  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sayaAdmin
January 29, 2012
Avoiding to show next page

Any way not show nextPageToPost.jsp after user submitting their inputs? I have post method into my html, then jump to php, but I don't want to show next page. Just want to show redirected no input page as it used to be before user made inputs.
azerty
February 1, 2012
Thanks

Thanks for the example
foo
April 27, 2013
typo error in form

this is using GET (default) instead of POST. form tag should be corrected as method="POST" instead of action="POST"
DMCA.com