login

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
<html>
<head>
<title>User Login</title>
</head>
<body style=”padding:20px”>
<form method=’post’ action=’/StartServlet/Authenticate’ name=’form1′>
<input type=’text’ name=’username’ placeholder=’User Name’ /><br><br>
<input type=’Password’ name=’password’ placeholder=’Password’ /><br><br>
<input type=’submit’ value=’User Login’ />
</form>
</body>
</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
simple-servlet-program
  • 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.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Login extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
String username=”Belal”;
String password=”khan”; PrintWriter out = response.getWriter(); String uname=request.getParameter(“username”);
String pass=request.getParameter(“password”); String Succeed = “<html><head><title>Welcome”+username+”</title></head>”;
Succeed += “<h1><body>Successfully Logged In</h1>”;
Succeed += “<h2>Your User Name: “+uname+”</h2><br>”;
Succeed += “<h2>Your Password: “+pass+”</h2></body><br>”;

String Failure = “<html><head><title>Can’t Log In</title></head>”;
Failure += “<h1><body>Invalid Login Credentials</h1>”;

if(uname.equals(username) && pass.equals(password))
{
out.println(Succeed);
}
else
{
out.println(Failure);
}
}
}

  • 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
<web-app>
<servlet>
<servlet-name>User Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>User Login</servlet-name>
<url-pattern>/Authenticate</url-pattern>
</servlet-mapping>
</web-app>
  • 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
http://localhost:<your-port-number>/<your-app-directory>/<html-file>
  • In my case the url is
http://localhost:8085/StartServlet
  • 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

simple-servlet-program1
  • 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
 simple-servlet-program2
  • 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
 simple-servlet-program3
  • 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.
http://surfpk.com/wp-content/uploads/2015/09/simple-servlet-program1.pnghttp://surfpk.com/wp-content/uploads/2015/09/simple-servlet-program1-150x150.pngSurfPK.comHelp&HowToJava TutorialSoftwaresTips & Trickshtml form,Java Tutorial,manipulate data,servlet program
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...