import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class MyCookieServlet extends HttpServlet implements SingleThreadModel { private static final String name = "JavaGuyLogin"; private static final String form = "
" + "New Visitor Name:
" + "Return Visitors just
"; private static final String message = "" + "

Since I have deemed that revealing this information is harmless," + "

You may view the Visitor Names file." + "

You may view the Site Log file." + "

If you delete your cookie you will have to choose a new name!" + "

If you want to change your name you will have to delete your cookie." + "

To delete your cookie, choose 'find' then 'files' from the start menu." + "
Set named: to '*.txt'.
Set containing text: to 'your old name'." + "
Set look in: to 'c:' and be sure that include subfolders is checked." + "
Click the 'advanced' tab and set size is: to 'at most 1k' and click 'find now'." + "
Then just 'right' click on the file and choose 'delete'.
"; String guest; String list; String value; String welcome; String dateTime; Date date; Cookie theCookie; Cookie[] cookies; LinkedList theNames; BufferedReader in; FileIO names; FileIO log; boolean validCookie,flag; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { validCookie = false; flag = false; names = new FileIO("../webapps/examples/ScreenNames.txt"); value = ServletUtilities.filter(request.getParameter("cookievalue")); cookies = request.getCookies(); welcome = "Welcome " + value; if (cookies != null) { for(int i=0, n=cookies.length; i < n; i++) { theCookie = cookies[i]; if (theCookie.getName().equals(name)) { validCookie = true; value = theCookie.getValue(); welcome = "Welcome back " + value; } } } if ((validCookie == false)&&(value.length() != 0)) { theNames = new LinkedList(); list = names.read(); in = new BufferedReader(new StringReader(list)); while((guest = in.readLine()) != null ) { theNames.add(guest); if (value.equalsIgnoreCase(guest)) { flag = true; response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("" + "NameTaken" + "" + "Sorry but that name is taken" + form + message); out.close(); } } in.close(); if (flag == false) { theNames.add(value.toLowerCase()); } Collections.sort(theNames); list = new String(); for(int i = 0;i < theNames.size();i++) { list += theNames.get(i) + "\n"; } names.write(list); } theCookie = new Cookie (name,value); theCookie.setMaxAge (60*60*24*356); // Generate Output response.setContentType("text/html"); PrintWriter out = response.getWriter(); if(value.equals("")) { out.println("" + "CookieExpired" + "" + "Your cookie must have expired" + form + message); } else if (flag == false) { log = new FileIO("../webapps/examples/SiteLog.txt"); date = new Date(); dateTime = DateFormat.getDateTimeInstance().format(date); log.append(value + " logged in " + dateTime + " Pacific"); // Save cookie response.addCookie(theCookie); out.println("" + "Welcome" + "

" + welcome + "

You may now enter JavaGuy.yi.org" + message); } out.close(); } }