set-cookie.jsp
<% { String u = request.getParameter("username"); String p = request.getParameter("password"); if(u.equals("u1") && p.equals("p1")) { Cookie info1 = new Cookie("up", u+'/'+p); info1.setPath("/"); info1.setMaxAge(60*10); response.addCookie(info1); response.sendRedirect("http://dlsun1710/dataSet_form1/protected.jsp"); } else response.sendRedirect("http://dlsun1710/dataSet_form1/login.html"); } %>
protected.jsp
<%@ page import="javax.servlet.http.*" %> <% { Cookie cookies[] = request.getCookies(); if(cookies != null) { for (int i=0; i<cookies.length; i++) { if(cookies[ i].getName().equals("up")) { if(cookies[ i].getValue().equals("u1/p1")) response.sendRedirect("http://dlsun1710/dataSet_form1/success.html"); else response.sendRedirect("http://dlsun1710/dataSet_form1/login.html"); } } } response.sendRedirect("http://dlsun1710/dataSet_form1/login.html"); } %>