package int_2.servlets.assignments4; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import int_2.servlets.*; import int_2.tools.*; import int_2.beans.*; /** Please respect the copyright of this code. It is not allowed to copy this code and use at any circumstances. @author © Fredrik Andersson
This servlet uses code from my home made toolbox that you can find in the folder 8/tools and 8/beans. The toolbox is not ready yet, I have been working for it for a couple of years when I have time. The goal is to get a toolbox of classes so it will be easier to map tasks to use-case-diagram. This servlet sets 2 secure cookies and then if the user wants it will read the cookies. To get this to work I hade to configure my orion server with a test server certificate. And I also needed to accept this ceritificate in the browser. */ public class SessionSecureCookies_4d extends GeneralServlet { public void start(HttpServletRequest request, HttpServletResponse response) throws IOException { Form form = FormManager.collectForm(request); String output = ""; if(form == null || form.get("back") != null) { output = createPage("applications/int_2/int_2/templates/session_secure_cookies1.html", "session_secure_cookies1"); printString(output); flush(); } else if(form.get("create") != null) { Cookie[] cookies = request.getCookies(); if(cookies != null) { boolean timeFound = false; boolean nameFound = false; for(int i = 0; i < cookies.length; i++) { if(cookies[i].getName().equals("int_2_time_secure")) { try { cookies[i].setValue(new java.util.Date().toString()); cookies[i].setMaxAge(10800); cookies[i].setSecure(true); response.addCookie(cookies[i]); timeFound = true; } catch(Exception e) { e.printStackTrace(); } } else if(cookies[i].getName().equals("int_2_name_secure")) { try { cookies[i].setValue((String)form.get("cookie_value")); cookies[i].setMaxAge(10800); cookies[i].setSecure(true); response.addCookie(cookies[i]); nameFound = true; } catch(Exception e) { e.printStackTrace(); } } if(!timeFound) { Cookie cookie1 = new Cookie("int_2_time_secure", new java.util.Date().toString()); cookie1.setMaxAge(10800); cookie1.setSecure(true); response.addCookie(cookie1); } if(!nameFound) { Cookie cookie2 = new Cookie("int_2_name_secure", (String)form.get("cookie_value")); cookie2.setMaxAge(10800); cookie2.setSecure(true); response.addCookie(cookie2); } } } else { Cookie cookie1 = new Cookie("int_2_time_secure", new java.util.Date().toString()); cookie1.setMaxAge(10800); cookie1.setSecure(true); response.addCookie(cookie1); Cookie cookie2 = new Cookie("int_2_name_secure", (String)form.get("cookie_value")); cookie2.setMaxAge(10800); cookie2.setSecure(true); response.addCookie(cookie2); } output = createPage("applications/int_2/int_2/templates/session_secure_cookies2.html", "session_secure_cookies2"); printString(output); flush(); } else if(form.get("read") != null) { output = createPage("applications/int_2/int_2/templates/session_secure_cookies3.html", "session_secure_cookies3"); StringBuffer stringBuffer = new StringBuffer(); Cookie[] cookies = request.getCookies(); if(cookies != null) { for(int i = 0; i < cookies.length; i++) { //if(cookies[i].getSecure()) //{ stringBuffer.append("
"); stringBuffer.append(cookies[i].getName()); stringBuffer.append(" : "); stringBuffer.append(cookies[i].getValue()); //} } } else { stringBuffer.append("There is no cookies!"); } output = TextManager.replaceInString(output, "XXXCOOKIELISTXXX", stringBuffer.toString()); printString(output); flush(); } } }