/* * Created on August 11, 2005, 9:38 AM */ package com.mycompany; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author JohnN * @version */ /* This servlet uses cookies to store data entered in the form by the user from the ITProducts.Com Registration Page and to make the form data available for display. The original page displayed by the servlet is the ITProducts.Com Registration Page, which displays the form. If the user presses Submit from the ITProducts.Com Registration Page, error checking is performed to determine whether data is entered and whether entered data is correct. - If so, the data is displayed in the ITProducts.Com Confirmation Page. From here, the user can click Edit to return to the form. The form is now called the ITProducts.Com Edit Page. - If not, the form is redisplayed with any entered data and appropriate error messages. The form is now called the ITProducts.Com Edit Page. If the user presses Reset from the ITProducts Registration Page, the ITProducts.Com Registration page is redisplayed with all form data restored to their original values. */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CookieSelfHandlingForm extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Create variables // The value of pagetype is either "Registration" or "Edit" // The Registration page is the form when first displayed or after Reset is pressed // The Edit page is the same form after Submit is pressed and error messages are displayed // Note that if Submit is pressed and all values are entered correctly, // a new Confirmation page is displayed by the doPost method String pagetype; // Variables for form data String username, email, occupation, region, otherregionname, newproducts, specialoffers; // The value of these variables control what happens in the doPost method String reset, submit; // Initialize variables pagetype = ""; username = null; email = null; occupation = ""; region = ""; otherregionname = null; newproducts = ""; specialoffers = ""; reset = ""; submit = ""; res.setContentType("text/html"); PrintWriter out = res.getWriter(); // Set up a cookie array Cookie[] cookies = req.getCookies(); // See if there are any cookies in array, that is, check that array is not null try { // If cookies in array, then this is the Edit page // Obtain all values for (int i = 0; i "); out.println(""); out.println("ITProducts.Com " + pagetype + " Page"); out.println(""); out.println(""); out.println("

ITProducts.Com " + pagetype + " Page

"); out.println("
"); out.println(""); // Username out.println(""); out.println(""); out.print(""); out.println(""); // Error message if no username entered if (submit.equals("submit") && username.equals("")) { out.println(""); out.println(""); out.println(""); out.println(""); } // Email out.println(""); out.println(""); out.print(""); out.println(""); // Error message if no email address entered if (submit.equals("submit") && email.equals("")) { out.println(""); out.println(""); out.println(""); out.println(""); } // Occupation out.println(""); out.println(""); out.println(""); out.println(""); // Error message if no occupation chosen if (submit.equals("submit") && occupation.equals("choose")) { out.println(""); out.println(""); out.println(""); out.println(""); } out.println("
Name:"); out.println("
Please type your username.
Email address:"); out.println("
Please type your email address.
Occupation:
Please choose your occupation.
"); // Region out.println("

"); out.println("Region:
"); out.println("North America
"); out.println("Asia Pacific
"); out.println("Europe
"); out.println("Other, please specify"); out.println(""); out.println("

"); // Error message if region combination incorrect if (submit.equals("submit")) // The Submit button was pressed { if (region.equals("")) // No region was selected { if (otherregionname.equals("")) // No region name was entered out.println("

Please click either a specific Region or Other.

"); else // A region name was entered out.println("

If you type another region name, you must also click Other.

"); } else if (region.equals("otherregion")) // Other was selected { if (otherregionname.equals("")) // A region name was not entered out.println("

If you click Other, you must also type the region name.

"); } else // A specific region was selected { if (!otherregionname.equals("")) // A region name was entered out.println("

If you click a specific Region, you cannot type another region name.

"); } } // New Products out.println("

"); out.println("Subscribe to new products email list.

"); // Special Offers out.println("

"); out.println("Subscribe to special offers email list."); out.println("

"); out.println("

"); // Genuine Submit button to submit the form // The button is trapped and error checking is performed is doPost method out.println(""); // Fake Reset button (using a submit action) to reset the form // The button is trapped and values are set to defaults in doPost method out.println(""); out.println("

"); out.println(""); out.println(""); out.println(""); out.close(); } public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Create variables String pagetype; String username, email, occupation, region, otherregionname, newproducts, specialoffers; String reset, submit; String newURL = res.encodeRedirectURL("CookieSelfHandlingForm"); // Get values of form data pagetype = req.getParameter("pagetype"); username = req.getParameter("username"); email = req.getParameter("email"); occupation = req.getParameter("occupation"); region = req.getParameter("region"); if (region == null) region = ""; otherregionname = req.getParameter("otherregionname"); newproducts = req.getParameter("newproducts"); if (newproducts == null) newproducts = ""; specialoffers = req.getParameter("specialoffers"); if (specialoffers == null) specialoffers = ""; reset = req.getParameter("reset"); if (reset == null) reset = ""; submit = req.getParameter("submit"); if (submit == null) submit = ""; // Create cookies from form data, to expire when session finishes Cookie myCookie; myCookie = new Cookie("pagetype",pagetype); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("username",username); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("email",email); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("occupation",occupation); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("region",region); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("otherregionname",otherregionname); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("newproducts",newproducts); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("specialoffers",specialoffers); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("reset",reset); myCookie.setMaxAge(-1); res.addCookie(myCookie); myCookie = new Cookie("submit",submit); myCookie.setMaxAge(-1); res.addCookie(myCookie); // Trap Submit button and do some error checking if (submit.equals("submit") // The Submit button was pressed && (username.equals("") || // No user name was entered email.equals("") || // No email address was entered occupation.equals("choose") || // No occupation was selected region.equals("") || // No region was selected // Other was selected but a region name was not entered (region.equals("otherregion") && otherregionname.equals("")) || // A specific region was selected and a region name was entered (!region.equals("") && !region.equals("otherregion") && !otherregionname.equals("")))) res.sendRedirect(newURL); // Trap Reset button and explicitly set form values to defaults by expiring all cookies if (reset.equals("reset")) { myCookie = new Cookie("pagetype",pagetype); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("username",username); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("email",email); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("occupation",occupation); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("region",region); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("otherregionname",otherregionname); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("newproducts",newproducts); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("specialoffers",specialoffers); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("reset",reset); myCookie.setMaxAge(0); res.addCookie(myCookie); myCookie = new Cookie("submit",submit); myCookie.setMaxAge(0); res.addCookie(myCookie); // Using setStatus and setHeader because using res.sendRedirect caused // an IllegalStateException in Apache Tomcat 4.0.4 if the Reset button // was clicked when all field were set to their default values. // See http://www.jguru.com/faq/view.jsp?EID=53251 res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.setHeader("Location",newURL); } res.setContentType("text/html"); PrintWriter out = res.getWriter(); // HTML document out.println(""); out.println(""); out.println("ITProducts.Com Confirmation Page"); out.println(""); out.println(""); out.println("

ITProducts.Com Confirmation Page

"); out.println("

You provided the following information:

"); // Username out.print("

Name: "); out.print(""); out.print(username); out.print(""); out.println("

"); // Email out.print("

Email: "); out.print(""); out.print(email); out.print(""); out.println("

"); // Occupation out.print("

Occupation: "); out.print(""); if (occupation.equals("choose")) // Not really needed out.print(""); else if (occupation.equals("prog")) out.print("Programmer"); else if (occupation.equals("netadmin")) out.print("Network Administrator"); else if (occupation.equals("webdev")) out.print("Web Developer"); else if (occupation.equals("itman")) out.print("IT Manager"); else if (occupation.equals("other")) out.print("Other"); out.print(""); out.println("

"); // Region out.print("

Region: "); out.print(""); if (region.equals("usregion")) out.print("North America"); else if (region.equals("apregion")) out.print("Asia Pacific"); else if (region.equals("euregion")) out.print("Europe"); else if (region.equals("otherregion")) if (otherregionname.equals("")) // Not really needed out.print("Other region, not specified"); else out.print(otherregionname); out.print(""); out.println("

"); // New Products out.print("

Subscribe to new products email list: "); out.print(""); if (newproducts.equals("on")) out.print("Yes"); else out.print("No"); out.print(""); out.println("

"); // Special Offers out.println("

Subscribe to special offers email list: "); out.print(""); if (specialoffers.equals("on")) out.print("Yes"); else out.print("No"); out.print(""); out.println("

"); out.println("

"); out.print(""); out.print("Edit"); out.print(""); out.println("

"); out.println(""); out.println(""); out.close(); } }