import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * Ask user to sign in on first visit. * Welcome user back, using sign-in name, on later visits. */ public class Greeting extends HttpServlet { /** * Respond to any HTTP GET request with either a sign-in or a * welcome-back page, depending on whether or not this is the first * visit. */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set the HTTP content type in response header response.setContentType("text/html; charset=\"UTF-8\""); // Obtain a PrintWriter object for creating the body // of the response PrintWriter servletOut = response.getWriter(); // If first visit by user, display sign-in form. Otherwise, // welcome the returning visitor back using name stored in the // session object on the first visit. HttpSession session = request.getSession(); String signIn = (String)session.getAttribute("signIn"); if (session.isNew() || (signIn == null)) { printSignInForm(servletOut, "Greeting"); } else { printWelcomeBack(servletOut, signIn); } // Clean up and return servletOut.close(); } /** * Respond to any HTTP POST response with a thank you, and store * the sign-in name supplied in the session object. */ public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set the HTTP content type in response header response.setContentType("text/html; charset=\"UTF-8\""); // Obtain a PrintWriter object for creating the body // of the response PrintWriter servletOut = response.getWriter(); // If signIn parameter present in request, output "thank you" // message and store sign-in within session object. // Otherwise, an unexpected behavior has occurred; display // signIn form again. String signIn = request.getParameter("signIn"); HttpSession session = request.getSession(); if (signIn != null) { printThanks(servletOut, signIn, "Greeting"); session.setAttribute("signIn", signIn); } else { printSignInForm(servletOut, "Greeting"); } // Clean up and return servletOut.close(); } /** * Create page containing form requesting user to sign in. */ private void printSignInForm(PrintWriter servletOut, String action) { printStart(servletOut); servletOut.println( "
\n"); printEnd(servletOut); } /** * Create a page thanking the user for signing in */ private void printThanks(PrintWriter servletOut, String signIn, String href) { printStart(servletOut); servletOut.println( "\n" + " Thanks for signing in, " + WebTechUtil.escapeXML(signIn) + ". \n" + "
\n" + "\n" + " Please visit again! \n" + "
\n"); printEnd(servletOut); } /** * Create a page welcoming the user back by name */ private void printWelcomeBack(PrintWriter servletOut, String signIn) { printStart(servletOut); servletOut.println( "\n" + " Hey, you're " + WebTechUtil.escapeXML(signIn) + ", aren't you? \n" + " Welcome back!! \n" + "
\n"); printEnd(servletOut); } /** * Output initial portion of response */ private void printStart(PrintWriter servletOut) { servletOut.println( " \n" + " \n" + " \n" + "