Thursday, 13 October 2016

JSP Servlet Login Logout Register Application MVC Approach - Part 2

Hello everybody!

Welcome to the second part of the post! You can see the Part 1 here. Let's get on with the project.

We can now create all the jsp pages. Some of these pages will be page templates and some of them will be sending form information to the Servlets that we created in the previous post.



"index.jsp" will be the first page that will be displayed when we run the project. As you can see below, "index.jsp" includes another jsp page that is called "link.jsp". Here in this project I am using the "link.jsp" as a template page. (Well sort of!) So that I can include it from multiple jsp pages throughout the project. You will notice as we continue anyway :)
index.jsp
link.jsp
"link.jsp" page has links to other jsp pages that are "login.jsp" and "register.jsp". Both of these pages have form elements in them to get information from users. They send all the form information to the servlets that are provoked by the "action" tag. For example "login.jsp" sends information to "Login" and "register.jsp" sends information to "Register" servlet. As you can remember from the previous post, these servlet names are defined in web.xml file.
login.jsp
In the "register.jsp" page, before sending all the user information to the servlet, I added a control for mail validation. The "onClick" attribute of the "Register" button calls checkMail() javascript function which is located under "resources/js". You can see the content of the "script.js" file below.
register.jsp
script.js
Filling out the login form, the user is directed to the "welcome.jsp" page if login is successful. This decision is made by the "Login Servlet" as we all know already. If there is a problem occurred in the servlet side, the user is directed to the login.jsp page once again. But this time with an error message. In "login.jsp", we display this error message by using the same name(${errorLogin}) as defined in the servlet. You can see the "welcome.jsp" page below. In the "welcome.jsp" page, we need to be sure that there is actually an active user to be welcomed :D So that is why we need to check this using a session control filter(explanation about filters is later in this post). If everything is okay with the session, then we can reach the "welcome.jsp" page and get the user information from the session object.
welcome.jsp
As you can see above, users have two options in the "welcome.jsp" page. They either go to "profile.jsp" page or log out. I added form elements around these buttons because I want to invoke related servlets to do the necessary actions. I do not know if this is the best way to do it. I will be checking this! So here is "profile.jsp" and "logout.jsp" pages. Note that these pages can only be viewed because ProfileServlet and LogoutServlet directed the user to these pages.

In the "profile.jsp" page, we get the active user information in the session by saying "session.getAttribute("activeUser"). Of course before that, we must know that whether there is an active session. To be able to check this, I used a filter which I am going to talk about later in this post.

Also in the profile.jsp, I have used JSTL(JSP Standard Tag Library). I already have had a blog post about JSTL. You can view it from here. There is a detailed enough explanation in that other post. So I am not going to mention about JSTL all over again.

profile.jsp
logout.jsp
If a user wants to register, he will need to click the "Register" link on the index.jsp page(includes link.jsp). This link will take him to "register.jsp" page that we've already seen above in this post. When the user fills out the registration form and click the "Register" button, all the information provided by the user will be send to "RegisterServlet". This servlet will check if everything is okay. If okay the user will see the "registersuccess.jsp" page.(All the details about these servlets are in the previous post!). If there is a problem occurred, the user will be sent back to "register.jsp" with an error message(${errorRegister}). In "register.jsp", we display this error message by using the same name(${errorRegister}) as defined in the related servlet.
registersuccess.jsp
This is all with the jsp pages. As I mentioned before in this post,I used filters to control session status and cache status throughout the project. You can see the "SessionControlFilter.java" below. It checks the session object and determines whether it is null or not. If it is null(that means no active user in the session), it redirects to index.jsp page. Otherwise the project flow continues as normal.
SessionControlFilter.java
The other filter is the "NoCacheFilter.java". It stops browsers to cache user data. Imagine a logged in user first logs out from the system and then click the back button in the browser. If we don't do anything for this, the user can still see authorisation required pages even when he actually logged out. This is down to browser cache system. So by using NoCacheFilter we want to stop this happening.
NoCacheFilter.java
I can hear the question coming. That is okay but how do these filters work?  Well we declare all definitions about our filters in the web.xml file. you can see how I defined these filters in the web.xml file below. In the "web.xml" file we define the filters location and give this filter a name such as NoCacheFilter and SessionControlFilter. Then we tell the filter which url pattern that it needs to effect. So whenever this url pattern called throughout the project, the filter that is responsible for this url pattern will do its job first. The user will be directed to somewhere according to filter's decision.

I will the posting the third and last part of these posts soon.

Hope to see you in the next blog post!

Selen

No comments:

Post a Comment