Handling user inputs using Servlets - Part 1.

In most of the websites and in all web applications there are forms for taking input from users. You may have seen form in "Contact Us" section of websites. Even if you are surfing on internet there are forms that you fills up for searching things on internet.

Forms are the most effective way of taking inputs from user. In this post we are going to discuss how we can handle forms using servlets.

In order to start we need to create a form that either can be in form of HTML page or JSP page whatever you like. We need to use <form> tag for generating forms.


<form>
    .
    .
   //We will be using different elements within this form 
     element for taking different types of inputs from users.
    .
    .
</form>

There is <input> tag which can be used within form element for creating different types of controls for taking inputs from user.Below is the list of components that can be used in form for taking inputs from users.

Textbox : This is the most common element that is being used in forms for taking input from users. We will use input tag and will use its type attribute for generating a textbox by passing "text" in its value. Below is an example.

Enter Name : <input type="text"> 

Above code will generate below element on HTML page.
Textbox in Form

Dropdown : Another common element that we see in HTML forms is dropdown. For generating dropdown we will use <select> tag, this will generate dropdown in form. We can use <option> tag for specifying values in dropdown. Below is an example.

Select Favorite Color : <select>
                           <option>Red</option>
                           <option>Blue</option>
                           <option>Green</option>
                        </select>

Above code will generate below element on HTML page.
Select in Form







Radio Buttons : I am sure you all have seen radio buttons in web sites as well. We will use <input> tag and will pass radio in it's type attribute. Below is an example.

Gender : <input type="radio"> Male <input type="radio"> Female

Above code will generate below element on HTML page.
Radio buttons in Form

For more information on input tag you can visit on : W3Schools

Now we have essential information about form and input tags which we can utilize for generating forms on HTML page. For our exercise we will take "Sign Up" form as an example. Below is the code for form creation :

<form>
First Name : <input type="text">
<br/><br/>
Last Name : <input type="text">
<br/><br/>
Gender : <input type=radio> Male <input type=radio> Female
<br/><br/>
Favorite Color : <select>
                                 <option>Red</option>
                                 <option>Blue</option>
                                 <option>Green</option>                               
                          </select>
<br/><br/>
</form>

Above code will generate below form :

Forms in HTML










This is all about basics of HTML form. We will continue this topic in parts, this is the first part. Please let me know what you feel about this post.

Continue to Part 2
Share:

3 comments:

Popular Posts

Recent Posts

Followers

Total Pageviews