Hello everybody,
I recently developed a basic login logout register application using JSP and Servlets. This time I wanted to use MVC in the project structure. So that the project is easy to handle, extend and change. This will be relatively long post, so I will try to divide it into two or more parts.
The final project structure will be like below. You can see all the pages, packages, resource files and deployment descriptor file. I will try to explain each of those step by step. So please stay tuned! :)
|
Project Structure |
|
Expanded src Folder |
Let's start with the model. "User.java" is our model class located under "com.selen.model" package. It has all the variables and corresponding getter-setter methods.
You can see the details below.
|
User.java |
Once we sorted the model class out, it is time to get database connection and operations ready. Here is SQL code to create the USERS table.
|
SQL Code to Create USERS table |
DbUtil.java holds necessary code to set up a database connection. Details can be seen below.
|
DbUtil |
Under the package "com.selen.dao", I have "UserDao.java" which handles all the database operations. Inside of the "UserDao.java", there are three functions each of which has a specific job. First one is "addUser" function. This function simply gets all parameters and insert a new record to the database.
|
UserDAO addUser |
The second one is "getUserInfoLogin" function which takes username and password information and tries to find the corresponding record in the database. If there is a matching user profile in the database, the function will set the User object with user's information.
|
UserDAO getUserInfoLogin |
Third one is "getUserProfile" which is a similar function to the previous one. This function looks at the database to find a matching user name and sets the User object if it can find a record.
|
UserDAO getUserProfile |
Now it is time to work on Servlets which will handle all the business logic. Each servlet must be responsible for one specific task. So we are going to have LoginServlet, LogoutServlet, ProfileServlet and RegisterServlet to deal with possible user requests.
Starting with LoginServlet.java, it will get user name and password information and ask UserDao's getUserInfoLogin method to check them. If the method returns something rather than null, we will send the request to the welcome.jsp page. Otherwise user will be directed back to login.jsp page and get an error message that we set by saying "errorLogin".You can see LoginServlet.java below.
|
LoginServlet |
LogoutServlet.java is rather simple. It only kills the active session and redirects to logout.jsp page. See below for the details.
|
LogoutServlet |
In ProfileServlet.java, the servlet will ask UserDao's getUserProfile method to find the active user in the database and retrieve all of its information. If successful it will dispatch the request to profile.jsp page.
|
ProfileServlet |
Lastly RegisterServlet.java... This servlet will get all information provided by the user in the registration form. Then asking UserDao's addUser method, it will create a new user in the database. If everything goes well, the user will be directed to registersuccess.jsp page. Otherwise an error message(errorRegister) will be shown.
|
RegisterServlet |
Now we are done with the servlets but we still need to sort out the servlet definitions in the web.xml. I will add the whole view of the web.xml here, but actually there are some other definitions for welcome file, session timeout or filters which I will talk about later in this post. So for now just pay attention to the marked part of the web.xml.
|
web.xml (Servlet definitions) |
By the way, in this project I preferred to handle exceptions using scriplets. It is basically try-catch code. It is not the best way but sometimes this method can be used if fast debugging required. I am going to use "Error Pages" to handle exceptions in my next project.
This is all for the Part 1. You can keep reading the second part of this port from
here.
Hope to see you in the next blog post!
Selen
No comments:
Post a Comment