Here, we are gonna build a login screen which will take the hardcoded username and password value from servlet.
Before we proceed make sure you have following software up and running.
login.jsp
Upon successful login we are going to redirect the page to the success page. So, lets create the success page.
success.jsp
Now, lets create a servlet which will handle the client request. For this particular example only the doPost method is required however, I am writing doGet, init and destroy method as well just for the sake of batter understanding of the example. Under the src folder lets create the new servlet.
TestServlet.java
That is all required to run this example so, now go ahead and deploy the project on your tomcat server and the screen will look as per below sceenshot.
Enter the correct username and password
Now have a look at the console.
Related Post:
Before we proceed make sure you have following software up and running.
- Eclipse IDE
- Tomcat server
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Page</title> </head> <body> <form action="TestServlet" method="post"> <fieldset> <legend>User login</legend> <table> <tr> <td>User Name:</td> <td><input type="text" name="user" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="pass" /></td> </tr> <tr> <td><input type="submit" value="Login" /></td> </tr> </table> </fieldset> </form> </body> </html>
Upon successful login we are going to redirect the page to the success page. So, lets create the success page.
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Success Page</title> </head> <body> <h1 align="center">Welcome to the world of servlets!</h1> <h3 align="center" style="color: blue">Created by Amzi...</h3> </body> </html>
Now, lets create a servlet which will handle the client request. For this particular example only the doPost method is required however, I am writing doGet, init and destroy method as well just for the sake of batter understanding of the example. Under the src folder lets create the new servlet.
TestServlet.java
package com.java.amzi; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String redirect = "test.jsp"; private static final String success = "success.jsp"; public TestServlet() { super(); System.out.println("Inside the constructor of the servlet"); } public void init(ServletConfig config) throws ServletException { System.out.println("In the init method!"); } public void destroy() { System.out.println("The destroy method is called!"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Inside the doGet method"); String user = request.getParameter("user"); System.out.println(user); String pass = request.getParameter("pass"); if (user.equals("Admin") && pass.equalsIgnoreCase("passw0rd")) response.sendRedirect(success); else { System.out.println("inside the else part"); response.sendRedirect(redirect); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Inside the doPost method"); doGet(request, response); } }
That is all required to run this example so, now go ahead and deploy the project on your tomcat server and the screen will look as per below sceenshot.
Enter the correct username and password
Now have a look at the console.
Related Post:
Good info on this post. You have shared very valuable info here.
ReplyDeletejava servlet programming
This is really a nice and informative, containing all information and also has a great impact on the new technology.
ReplyDeleteBest JAVA Training institute in Chennai
Java Courses in Chennai