Hello Friends,
This is part II of jsp and servlets login example. Check Part I here.
Step 4) Now, we need to create a Servlet file for the redirection of the jsp pages. In the src folder under the package code2java, add the Servlet file and name it as LoginServlet. As discussed earlier this servlet file will get the user details from the Bean file and then it will create a Session for that user only if the user is a valid user. And accordingly it will redirect to the respective jsp pages. You can use the default code which Eclipse generates while Servlet creation, the only change you need to do is in doGet() method. The code for this is written below; add this code in LoginServlet.java file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package code2java; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { System.out.println("In the Login Servlet"); LoginBean user = new LoginBean(); user.setUserName(request.getParameter("uname")); user.setPassword(request.getParameter("password")); user = LoginDAO.login(user); if(user.isValid()) { HttpSession session = request.getSession(true); session.setAttribute("currentSessionUser",user); response.sendRedirect("LoginSuccess.jsp"); }else response.sendRedirect("LoginFailed.jsp"); } catch (Throwable exc) { System.out.println(exc); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } } |
Step 5) You can see in the above code we have created the object of the Bean class LoginBean.java so that we can set the user inputs to a specific value and then use these values for further action which will be performed by DAO. So, we will create a LoginBean class first and then the DAO.
Step 6) In the src folder, under the package code2java, create a java class, name it as LoginBean and add the below code. This contains the setters and getters for setting up the values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package code2java; //Data Encapsulation using Getters and Setters public class LoginBean { private String username; private String password; private String firstName; private String lastName; public boolean valid; public String getFirstName() { return firstName; } public void setFirstName(String newFirstName) { firstName = newFirstName; } public String getLastName() { return lastName; } public void setLastName(String newLastName) { lastName = newLastName; } public String getPassword() { return password; } public void setPassword(String newPassword) { password = newPassword; } public String getUsername() { return username; } public void setUserName(String newUsername) { username = newUsername; } public boolean isValid() { return valid; } public void setValid(boolean newValid) { valid = newValid; } } |
Step 7) Now create another class under code2java package, name it as LoginDAO and add the below code. This DAO gets the requested values from the database using the DB query, in this case sql query. This query is created as per the inputs provided by the user on the LoginPage which is a JSP file. And as per the result of the executed query it will return the boolean value to the LoginBean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package code2java; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class LoginDAO { static Connection currentCon = null; static ResultSet rs = null; public static LoginBean login(LoginBean bean) { Statement stmt = null; String username = bean.getUsername(); String password = bean.getPassword(); String searchQuery = "select * from users where uname='" + username + "' AND password='" + password + "'"; try { //connecting to the DB currentCon = ConnectionManager.getConnection(); stmt=currentCon.createStatement(); rs = stmt.executeQuery(searchQuery); boolean userExists = rs.next(); if (!userExists) { System.out.println("Username/Password entered is Incorrect or User doesnot Exists."); bean.setValid(false); } else if (userExists) { String firstName = rs.getString("FirstName"); String lastName = rs.getString("LastName"); System.out.println("Welcome " + firstName); bean.setFirstName(firstName); bean.setLastName(lastName); bean.setValid(true); } } catch (Exception ex) { System.out.println("Log In failed: An Exception has occurred! " + ex); } return bean; } } |
We have implemented the DAO to access the data from DB, but we have not yet created the required database. This we will see in the next tutorial.
Thanks,
Nikhil Naoghare.
Hey, Nikhil, nice job! Thanks for the effort. This is one of the most straightforward tutorials I’ve found.
One small – okay, not so small – point: you posted LoginServlet.java code twice, once where it belonged and then again for LoginDAO. Could you please add the LoginDAO.java code when you get a chance.
Thanks.
Hello Greg,
Thanks for checking out that mistake 🙂 I have changed the existing code for LoginDAO and added the code which is required and correct. Please have a check now.
Regards,
Nikhil.