Showing posts with label Form in HTML. Show all posts
Showing posts with label Form in HTML. Show all posts

Handling user inputs using Servlets - Part 4

This is the final part of "Handling user inputs using Sevlets". In the last post we have seen how we can create servlet which will handle our server side processing.
To begin with we need to create an HTML page which will hold our sign up form. We have created sign_up.html page.

<html>
<head>
<title>Sign Up</title>
</head>
<body>
<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" value="male"> Male <input type=radio name="gender" value="female"> Female
<br/><br/>
Favorite Color : <select name="color">
                    <option value="Red">Red</option>
                    <option 
value="Blue">Blue</option>
                    <option value="Green">Green</option>                           
                 </select>
<br/><br/>

<input type="submit" /> <input type="reset" />
</form>

</body>

</html>

When we run our application, setting this html as first welcome file in deployment descriptor (WEB-INF/web.xml) we will be able to see below page in browser.

HTML Form
HTML Form

Now we will create the servlet which will process our form at server end.

import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/sign_up.do")
public class SignUpServlet extends HttpServlet{

 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

         String firstName = request.getParameter("first_name");
         String lastName = request.getParameter("last_name");
         String gender = request.getParameter("gender"); 
         String color = request.getParameter("color"); 
         System.out.println("Form submitted by user with below value");
         System.out.println(firstName+" : "+lastName+" : "+gender+" : "+color);
    }
}


Above we have only override doPost method as we are submitting form using POST method. For now we will be printing form values on console. You can save these values to database as well but that's a separate topic which we will cover later.

When below form is submitted we will be able to see below values in console.

Form in HTML

getParameter in Servlet


This is how we handle user inputs in servlet. Thanks for seeing all my posts on "Handling user inputs using Servlets". There are more to come please follow my blog and give your suggestions, any suggestion given is valuable to me.
Share:

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:

Popular Posts

Recent Posts

Followers

Total Pageviews