Handling user inputs using Servlets - Part 2

This post is in continuation of  first part of  "Handling user inputs using Servlets", if you haven't seen that post, I will request you to see that post first before continuing this post.

In our first part of this topic, we have seen how to create form. In this post we will learn how form submission works and what are the important things that we need to take care during form submission. Below are some important attributes of form tag that we need to use for form submission.

action : This attribute is use to specify the destination of the form, where the information that users has filled up needs to be send. Below is an example :

<form action="sign_up.do">.............</form>

When we submit above form the information that we have filled up in form will be submitted to the page that we have mentioned in action attribute. In our example form will be submitted to sign_up.do, which is a servlet.

method : This attribute is use to specify how form will be submitted. There are primarily two HTTP methods which we basically use for form submission. They are GET and POST, below is the difference between two.

GET : We can use this method when there is non-confidential information in our forms because when form is submitted with GET method all information in the form will get shown to the user in the URL.

POST : We can use this method when there is confidential information in our forms because when form is submitted with POST method all information in the form will not be shown to the user in the URL. All those information will be send in request body.

Below are examples for both methods.

<form action="sign_up.do" method="get">.............</form>
<form action="sign_up.do" method="post">.............</form>


Now we are aware how a form is submitted to a servlet. There is one more important thing that we need to take care for proper form submission. When we use input, select or any other tag inside form element, we need to make sure they all have "name" attribute and should be given a suitable value because through this value we will be getting the form information on server side. All those input elements which do not have name attribute inside forms will not be available on server side, so this is an important point to remember.

Below is the complete for with above attributes which is ready to be submitted.

<form action="sign_up.do" method="post">
First Name : <input type="text" name="first_name">
<br/><br/>
Last Name : <input type="text" name="last_name">
<br/><br/>
Gender : <input type=radio name="gender"> Male <input type=radio name="gender"> Female
<br/><br/>
Favorite Color : <select name="color">
                                 <option>Red</option>
                                 <option>Blue</option>
                                 <option>Green</option>                             
                          </select>
<br/><br/>
</form>


Above code will generate the same form which we have seen in first post.

This is how we create forms in HTML. In next post we will see how we can get values filled by user in a form on server side in servlet. Please let me know if you have any doubts or questions, I will be happy to help you out.

Continue to Part 3
Share:

Related Posts:

No comments:

Post a Comment

Popular Posts

Recent Posts

Followers

Total Pageviews

5,030