import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
/**
* a Java servlet that tries to play with sessions a little
*
* @author Sharon Tuttle
* @version 3-27-13
*/
public class St10SessionExample extends HttpServlet
{
/**
* create an HTML5 document that calls this servlet's doPost
* method when its submit button is clicked.
*
* @param request object containing the GET request information
* from the browser
* @param response object containing the response that will
* be sent back to the browser - here containing
* a form with the doPost method as its action
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// get a new session object
HttpSession session = request.getSession(true);
String sessionId = session.getId();
response.setContentType("text/html");
ServletOutputStream getOut = response.getOutputStream();
String title = "Playing with sessions S13 Part 1";
session.setAttribute("this course", "CS 318");
session.setAttribute("course int", new Integer(318));
session.setAttribute("pageNumber", new Integer(1));
session.setAttribute("looky", new Double(13.3));
//double lookyVal = session.getAttribute("looky");
//double lookyVal = (double) session.getAttribute("looky");
//Double lookyVal = (Double) session.getAttribute("looky");
//Double lookyVal = session.getAttribute("looky");
Html318Helpers.beginHtml(getOut, title);
getOut.println("<h1> " + title +
"</h1>");
getOut.println("<p>Session id is: " + sessionId + "</p>");
getOut.println("<form method=\"post\" " +
"action=\"http://nrs-projects.humboldt.edu:8888/servlet/" +
"St10SessionExample\">");
getOut.println("<p> Enter a quantity: ");
getOut.println("<input type=\"text\" " +
"name=\"quantity\" value=\"0\" /> </p>");
getOut.println("<p> <input type=\"submit\" " +
"value=\"Send value\" />" );
getOut.println("</form>");
Html318Helpers.endHtml(getOut);
}
/**
* respond to the page generated by the doGet method,
* and show that session info has been maintained/
* is available.
*
* @param request object containing the POST request information
* from the browser
* @param response object containing the response that will
* be sent back to the browser - here containing
* a response showing some session info
*/
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 St10SessionExample doPost method!"
+ " S13";
Html318Helpers.beginHtml(postOut, title);
postOut.println("<h1> " + title +
" </h1>");
postOut.println("<p> Session id is: " + sessionId
+ "</p>");
postOut.println("<table>");
int quantity = Integer.parseInt(
request.getParameter("quantity"));
postOut.println(" <tr> <td> quantity: </td> <td>"
+ quantity + "</td> </tr>");
// what if I'd like this quantity to be seen "later"?
// ...then I'd better SAVE it as a session attribute!
session.setAttribute("quantity", new Integer(quantity));
session.setAttribute("pageNumber", new Integer(2));
String course = (String) session.getAttribute(
"this course");
postOut.println(" <tr> <td> this course: </td> <td>" +
course
+ " </td> </tr>");
// don't NEEED to cast here, because + with at least 1
// String operand will cause toString method of
// Object attribute to be called
postOut.println(" <tr> <td> course int: </td> <td>" +
session.getAttribute("course int")
+ " </td> </tr>");
postOut.println("</table>");
postOut.println("<form method=\"post\" " +
"action=\"http://nrs-projects.humboldt.edu:8888/servlet/" +
"St10SessionExample2\">");
postOut.println("<p> <input type=\"submit\" " +
"value=\"to part 2\"> </p>");
postOut.println("</form>");
Html318Helpers.endHtml(postOut);
}
}