In this tutorial you will learn about the Servlet Cookie Example and its application with practical example.
Servlet Cookie Example
In previous article we have learned about cookies, it’s methods and constructor. In this article we are going to implement this thing in our browser.
Let’s understand it by login logout example, we have to create some class. Follow the below steps.
index.html
First we need to create index.html file. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>W3adda Cookies Example</title> </head> <body> <h1>Welcome to Login w3adda Cookies example</h1> <a href="login.html">Login</a>| <a href="LogoutServlet">Logout</a>| <a href="ProfileServlet">Profile</a> </body> </html> |
login.html
Lets create login.html file which we are going to use and put the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<html> <head> <title>w3adda.com</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="LoginServlet" method="post"> Name:<input type="text" name="name"><br> Password:<input type="password" name="password"><br> <input type="submit" value="login"> </form> </body> </html> |
LoginServlet.java
From this Servlet java class we are going to add cookies to our web browser.
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 |
package ServletClass; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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("abc1234")){ out.print("You are successfully logged in!"); out.print("<br>Welcome to w3adda.com, "+name); Cookie ck=new Cookie("name",name); response.addCookie(ck); }else{ out.print("sorry, username or password error!"); request.getRequestDispatcher("login.html").include(request, response); } out.close(); } } |
ProfileServlet.java : This is the profile servlet class from which we will get the cookie. Check below code.
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 |
package ServletClass; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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); Cookie ck[]=request.getCookies(); if(ck!=null){ String name=ck[0].getValue(); if(!name.equals("")||name!=null){ out.print("<b>Welcome to w3adda Profile</b>"); out.print("<br>Welcome, "+name); } }else{ out.print("Please login first"); request.getRequestDispatcher("login.html").include(request, response); } out.close(); } } |
LogoutServlet.java : This servlet is used for logout the session and cookie’s age will be replaced by 0. Have a look on method for age of cookies which are discussed in previous article of cookies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package ServletClass; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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); Cookie ck=new Cookie("name",""); ck.setMaxAge(0); response.addCookie(ck); out.print("you are successfully logged out!"); } } |
Output:-
Lets run this example You will get output like below images.
Screen 1:-
This will be our index.html file output. If we click on profile this will give output like second image.
Screen 2:-
This will come when we directly click on Profile button.
Screen 3:-
This will come on when successfully logged in . now when we click on Profile button, we will get our cookies result. Check next screenshot.
Screen 4:-
When we have data in cookies. So this will come.
Screen 5:-
on Successfully logout the below image will appear.