Friday, 3 March 2017

Login & Logout Using Cookies in Servlet.

*Example of Login and Logout Cookie in Java.

~File Name:
     
      1) Login.Html
      2) link.html
      3)LoginServlet.java
      4)LogoutServlet.java
      5)ProfileServlet.java



     * Login.html


<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</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><br>
            Password:<input type="password" name="password"><br>          <br>
            <input type="submit" name="go">
     
        </form>
     
    </body>
</html>


  *Link.html


<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <a href="login.html">login</a> ||
        <a href="logoutServlet">logout</a> ||
        <a href="ProfileServlet"> view Profile </a>
        
    </body>
</html>

*LoginServlet.java



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 {
 @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        
        
        request.getRequestDispatcher("link.html").include(request, response);
        
        if(password.equals("ujash"))
        {
                out.println("<br><br>You have successfully login");
                out.println("<br>welcome ::"+name);
            
            Cookie ck=new Cookie("name",name);
            response.addCookie(ck);
        }
        else
        {
            out.println("<br><br>You have to must check password..Please check  password again");
        }
        out.close();
                }
}

*logoutServlet.java



import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author DELL
 */
@WebServlet(urlPatterns = {"/logoutServlet"})
public class logoutServlet extends HttpServlet {

    @Override
    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.println("<br><br>you have Successfull logout");
        
        out.close();
    }

}
    

  * ProfileServlet.java



import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(urlPatterns = {"/ProfileServlet"})
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.println("<br>Welcome to Your Profile ");
                out.println("<br>Welcome ::"+name);
            }
        }
        else
        {
                out.println("<br>You have no Login");
                      request.getRequestDispatcher("login.html").include(request, response);
                
                    }
    
            }
 }
    


                                                                        OutPut




                                                          *Profile View.

*Logout

*View Profile With Out Login.







Thanks 
Happy Coding.!!   ;)





No comments:

Post a Comment