We can bind the objects on HttpSession instance and get the objects by using setAttribute and getAttribute methods.
In the previous page, we have learnt about what is HttpSession, How to store and get data from session object etc.
Here, we are going to create a real world login and logout application without using database code. We are assuming that password is admin123.
Visit here for login and logout application using cookies only servlet login and logout example using cookies
In this example, we are creating 3 links: login, logout and profile. User can’t go to profile page until he/she is logged in. If user is logged out, he need to login again to visit profile.
In this application, we have created following files.
- index.html
- link.html
- login.html
- LoginServlet.java
- LogoutServlet.java
- ProfileServlet.java
- web.xml
File: index.html
File: link.html
File: login.html
File: LoginServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); request.getRequestDispatcher("link.html").include(request, response); String name=request.getParameter("name"); String password=request.getParameter("password"); if(password.equals("admin123")){ out.print("Welcome, "+name); HttpSession session=request.getSession(); session.setAttribute("name",name); } else{ out.print("Sorry, username or password error!"); request.getRequestDispatcher("login.html").include(request, response); } out.close(); } }
File: LogoutServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LogoutServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); request.getRequestDispatcher("link.html").include(request, response); HttpSession session=request.getSession(); session.invalidate(); out.print("You are successfully logged out!"); out.close(); } }
File: ProfileServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ProfileServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); request.getRequestDispatcher("link.html").include(request, response); HttpSession session=request.getSession(false); if(session!=null){ String name=(String)session.getAttribute("name"); out.print("Hello, "+name+" Welcome to Profile"); } else{ out.print("Please login first"); request.getRequestDispatcher("login.html").include(request, response); } out.close(); } }
File: web.xml
LoginServlet LoginServlet LoginServlet LoginServlet /LoginServlet ProfileServlet ProfileServlet ProfileServlet ProfileServlet /ProfileServlet LogoutServlet LogoutServlet LogoutServlet LogoutServlet /LogoutServlet
Output
If again you click on the profile link, you need to login first.
Simply desire to say your article is as astounding. The clarity in your post is just great and i can assume you’re an expert on this subject. Fine with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the gratifying work.