Simple Servlet Program to Manipulate Data of HTML Form
I just posted about creating servlet programming environment in your system in my previous article. Now in this post we will learn about manipulating data of client side using HTML forms. We will create a simple login page; we won’t be using a database in this program it is a simple login page only in which the user name and password is already stored in string variables in our code. So lets begin.
Getting started with this simple servlet program
- In the previous post we executed a simple servlet program successfully if you haven’t read the post or unaware about starting servlet programming then you must first read the previous post from here.
- Now if you are already done with it move ahead.
- I will be working on the same folder which I have created before. So I have my project directory named StartServlet
Creating an HTML form
- Create an HTML file named index.html (Although you can give any name if you want)
- This is my code you can copy paste directly to your index.html
- Here you can see there are two textfields with the attribute name=’username’ and name=’password’ this attribute will be used to take values from the java code.
- The output of the above html code is like this
- Out interface is ready now we need to program the java file to get the data from the client window and check for authentication.
Creating java servlet program
- Create a java file inside the classes folder which is inside your WEB-INF folder. The directory structure is fixed so it should not be changed.
- I created Login.java inside my classes folder.
- This is my code you can copy paste directly to your java file.
- Now compile this java file and a class named Login will be created inside the classes folder.
Creating web.xml file for servlet program
- Inside the WEB-INF folder we need to create an xml file to execute our program.
- Create a file named web.xml, naming is very important because it is fixed and case sensitive so be careful while naming your files.
- Here is my code for web.xml file you can copy paste from here
- Now we have all the files ready.
- Open Monitor Tomcat and then start the server by right clicking and start services from the tomcat icon from your taskbar.
- If your server is already running then restart the server (right click in the icon click stop services and then click start services)
Executing the servlet program
- Open your browser
- Type the url as
- In my case the url is
- I have given the html file name index.html so I don’t need to type the html file name because it is the root file and will be loaded automatically but if you have given a different name you need to type the html file name as well
Output of the servlet program
- This is the HTML form, as we know from the code that the username is ‘Belal and password is ‘khan’ So when we enter this username and password and click on login the if statement becomes true and it executes the code written inside it and we get the output as
- But when we try giving some other username and password the if statement becomes false and it executes the else part so it shows the output as
- This is the simple html code next time we will try to design a good user interface by implementing some css on it.
- If you have any confusion or doubts related to the code or procedure then feel free to leave your comments.