import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.io.*; /** * a Java servlet that hopefully receives the session information * from another servlet (St10SessionExample) * * @author Sharon Tuttle * @version 3-27-13 */ import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.io.*; public class St10SessionExample2 extends HttpServlet { /** * try to invalidate current sessions and redirect user to "proper" * login page - St10SessionExample via doGet -- * if they try to reach here via doGet * * @param request object containing the request information * from the browser * @param response object containing the response that will * be sent back to the browser - here a redirect * to St10SessionExample */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); session.invalidate(); //ServletOutputStream tryOut = response.getOutputStream(); //tryOut.println("moo"); response.sendRedirect("http://nrs-projects.humboldt.edu" + ":8888/servlet/St10SessionExample"); } /** * create an HTML document that shows session information * passed to it from the St10SessionExample servlet * * @param request object containing the request information * from the browser * @param response object containing the response that will * be sent back to the browser - here including * the session information it received */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); String sessionId = session.getId(); response.setContentType("text/html"); ServletOutputStream postOut = response.getOutputStream(); String title = "from St10SessionExample2 doPost method!"; String oldCourse = ""; int oldInt = 0; int oldQuant = 0; int prevPageNumber = 0; // grab previous values from session, if can if (session.isNew() == false) { oldCourse = (String) session.getAttribute("this course"); oldInt = ((Integer) session.getAttribute("course int")).intValue(); oldQuant = ((Integer) session.getAttribute("quantity")).intValue(); prevPageNumber = ((Integer) session.getAttribute("pageNumber")).intValue(); } // otherwise, SHOULDN'T be here -- redirect to first page! else { // notice, only get here if session WAS new -- no need // invalidate a new session, I think; response.sendRedirect("http://nrs-projects.humboldt.edu:8888/" + "servlet/St10SessionExample"); } Html318Helpers.beginHtml(postOut, title); // if reach here, give next page based on previous page number if (prevPageNumber == 2) { // set up for testing clicker question answers session.setAttribute("looky", new Double(13.3)); // the rest is the actual set-up for page 3 session.setAttribute("pageNumber", new Integer(3)); postOut.println("<h1> " + title + " - PAGE 3 </h1>"); postOut.println("<table>"); postOut.println("<tr> <td> Session id is: </td> <td>" + sessionId + "</td> </tr>"); postOut.println("<tr> <td> Quantity is: </td> <td>" + oldQuant + "</td> </tr>"); postOut.println("</table>"); postOut.println("<form method=\"post\" " + "action=\"http://nrs-projects.humboldt.edu:8888/servlet/" + "St10SessionExample2\">"); postOut.println("<p> Enter an amount: "); postOut.println("<input type=\"text\" " + "name=\"amount\" value=\"0\" />"); postOut.println("</p>"); postOut.println("<p> <input type=\"submit\" " + "value=\"Send value\" />"); postOut.println(" "); postOut.println("<input type=\"reset\" /> </p>\n"); postOut.println("</form>"); } else if (prevPageNumber == 3) { // testing clicker question answers (comment/uncomment as // needed //Double lookyVal = session.getAttribute("looky"); Double lookyVal = (Double) session.getAttribute("looky"); //double lookyVal = (double) session.getAttribute("looky"); //double lookyVal = session.getAttribute("looky"); // the rest is the actual set-up for page 4 postOut.println("<h1> " + title + " - PAGE 4 </h1>"); postOut.println("Session id is: " + sessionId + "<br /><br />"); postOut.println("<table>"); postOut.println(" <tr> <td> quantity: </td> <td>" + oldQuant + "</td></tr>"); postOut.println(" <tr> <td> this course: </td> <td>" + oldCourse + "</td></tr>"); postOut.println(" <tr> <td> course int: </td> <td>" + oldInt + "</td></tr>"); postOut.println(" <tr> <td> amount: </td> <td>" + request.getParameter("amount") + "</td></tr>"); postOut.println("</table>"); // now, end this session... session.invalidate(); } // IF get here -- and I HOPE we can't -- invalidate session, and // redirect to first page... else { session.invalidate(); response.sendRedirect("http://nrs-projects.humboldt.edu:8888/" + "servlet/St10SessionExample"); } // now, regardless of which page was created above, // finish the page... Html318Helpers.endHtml(postOut); } }