Cookie Serv Let
Cookie Serv Let
Cookie Serv Let
public class LoginServlet extends HttpServlet { final String VALID_NAME = "FTKI"; final String PASSWORD = "1234567890"; final String CODE_VALID = "3g09I8Lm7Dw4N"; // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //form HTML untuk login out.println("<html>"); out.println("<head>"); out.println("<title>Login Page</title>"); out.println("</head>"); out.println("<body>"); out.println("<center><h1>CookiesServlet</h1></center>"); out.println("<p><h3>Setelah login, anda dapat melihat isi web kami!</h3></p>"); out.println("<p>Silahkan login disini:</p>"); out.println("<form name='login' action='LoginServlet' method='POST'>"); out.println("<p>Nama anda: "); out.println("<input type='text' name='userName' value='' width='24' />"); out.println("</p><p>Password: ");
out.println("<input type='password' name='userPwd' value='' width='24' />"); out.println("</p><br>"); out.println("<input type='reset' value='Reset' name='reset' /> "); out.println("<input type='submit' value='Login' name='submit' />"); out.println("</form>"); out.println("</body>"); out.println("</html>"); out.close(); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //ambil parameter nama dan password String userName = request.getParameter("userName"); String password = request.getParameter("userPwd"); boolean valid = false; //cek identitas login if ((userName.equalsIgnoreCase(VALID_NAME)) && (password.equals(PASSWORD))) { valid = true; } response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); if (!valid) { out.println("<html>"); out.println("<head>"); out.println("<title>Halaman Tampilan Kesalahan Login</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Invalid logins!</h1><br>"); out.println("<h2><a href=\"LoginServlet\">Back...</a></h2>"); out.println("</body>"); out.println("</html>"); out.close(); return; }
//buat cookie Cookie ckName = new Cookie("Pref", userName); Cookie ckPwd = new Cookie("Vid", CODE_VALID); ckName.setMaxAge(60*60); // 1 jam ckPwd.setMaxAge(60*60); // 1 jam response.addCookie(ckName); response.addCookie(ckPwd); //link ke halaman content out.println("<html>"); out.println("<head>"); out.println("<title>Content Link</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Login sukses!</h1><br>"); out.println("<h3>Klik tombol dibawah ini untuk masuk ke secret area...</h3>"); out.println("<form name='secret' action='SecretServlet' method='POST'>"); out.println("<p><input type='submit' value='Enter' name='submit' /></p>"); out.println("</form><hr>"); out.println("</body>"); out.println("</html>"); out.close(); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Login Servlet"; } // </editor-fold> } FILE SECRETSERVLET.JAVA : /* * SecretServlet.java * * Created on June 25, 2006, 12:24 PM */ package com.nawolo.cookieservlet; import java.io.*; import java.net.*; import javax.servlet.*;
import javax.servlet.http.*; /** * * @author soesilow * @version */ public class SecretServlet extends HttpServlet { final String VALID_NAME = "FTKI"; final String PASSWORD = "1234567890"; final String CODE_VALID = "3g09I8Lm7Dw4N"; // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //GET tidak melayani request out.println("<html>"); out.println("<head>"); out.println("<title>Not Authorized</title>"); out.println("</head>"); out.println("<body>"); out.println("<h3>You have no authorization.</h3>"); out.println("</body>"); out.println("</html>"); out.close(); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //ambil cookie dan cek Cookie[] cookies = request.getCookies(); String name1 = "", value1 = "";
String name2 = "", value2 = ""; try { name1 = cookies[0].getName(); value1 = cookies[0].getValue(); name2 = cookies[1].getName(); value2 = cookies[1].getValue(); } catch (Exception e) { e.printStackTrace(out); } boolean validCookie = false; if ((((name1.equals("Pref")) && (value1.equals(VALID_NAME)))) && (((name2.equals("Vid")) && (value2.equals(CODE_VALID))))) { validCookie = true; } else { out.println("<html>"); out.println("<head>"); out.println("<title>Tidak ada Otoritas</title>"); out.println("</head>"); out.println("<body>"); out.println("<h3>Anda tidak memiliki Otoritas.</h3>"); out.println("</body>"); out.println("</html>"); out.close(); return; } //Secret pages... out.println("<html>"); out.println("<head>"); out.println("<title>Secret Pages</title>"); out.println("</head>"); out.println("<body>"); out.println("<h3>This is secret pages...</h3>"); out.println("<p>Available only to our members...</p>"); out.println("<p>Please feel free to browse our contents...</p>"); out.println("</body>"); out.println("</html>"); out.close(); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Secret pages service.";
} // </editor-fold> } OUTPUT :