As mentioned in the application description, the user will have to enter his username and password, so first of all, we need a JSP that asks the user to input his username and password in their corresponding fields.
To have this JSP, please follow these steps:
As you can see; (in the LoginPage) when the user submits, the JSP calls "LoginServlet".This LoginServlet is intended to handle the Business logic associated with the request.
Create the LoginServlet by following these steps:
-
In the "src" folder, create a new "Package"
- Name it "ExamplePackage"
-
In the "ExamplePackage", create a new "Servlet"
- Name it "LoginServlet"
- Place this code
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 {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try
{
UserBean user = new UserBean();
user.setUserName(request.getParameter("un"));
user.setPassword(request.getParameter("pw"));
user = UserDAO.login(user);
if (user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("userLogged.jsp"); //logged-in page
}
else
response.sendRedirect("invalidLogin.jsp"); //error page
}
catch (Throwable theException)
{
System.out.println(theException);
}
}
}
The login servlet instantiates a Bean that is of type "UserBean", and then calls the DAO named "UserDAO".
-
Our UserBean is a class representing the User table in our Database (where each column in the user table has a corresponding instance variable with a setter and a getter method).
-
The DAO, as said before, contains methods needed to communicate with the data source. In our example, the only needed method is the login method that checks if the username and password inputted by the user are valid or not.
Before implementing the DAO, you need to prepare your Data Source.
- Create a table in your DB
- Name it users
-
Create the columns: 'FirstName', 'LastName', 'username', and 'password'
-
Refer to your DB as a data source from "Administrative tools" in Control Panel
Please follow these steps to implement the Bean and the DAO
- Create the "UserBean" class
-
In the " ExamplePackage ", create a new "Class"
- Name it "UserBean"
- Place this code
public class UserBean {
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;
}
}