An example of using graphics in servlets
Note that this example does not allow erasing the selected ice cream.
File IceCreamGraphics.java
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class IceCreamGraphics extends HttpServlet {
HttpSession session;
String kind;
Order ord;
// the user gets to the page without submitting a form
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
// session is retrieved before getting the writer
session = request.getSession(true);
// see what has been ordered already
Vector ordered = (Vector) session.getAttribute("order");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
out.println("<title> An ice cream shop </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Welcome to our ice cream shop!</h1>");
out.println("<IMG SRC = \"/examples/images/Food-related5.jpg\"><P>");
// print out what has been ordered
if (ordered == null && kind == null) {
session.setAttribute("order", new Vector());
out.println("You have not ordered anything yet.<BR>");
out.println("Try our ice cream, it's delicious!<P>");
}
else if (ordered == null) {
// add the current order to the new order list
Vector v = new Vector();
v.add(new Order(kind));
session.setAttribute("order", v);
}
else if (kind != null) {
ordered.add(new Order(kind));
session.setAttribute("order", ordered);
}
kind = null;
if ( ordered != null) {
out.println("You have ordered:<P>");
// extract all orders
Enumeration orders = ordered.elements();
while (orders.hasMoreElements()) {
out.println(((Order)orders.nextElement()).getName());
out.println("<P>");
}
}
out.println("Please click on ice cream you want to buy<BR>");
out.println("to add it to your order<P>");
// a button for vanilla ice cream
out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("IceCreamGraphics"));
out.print("\" ");
out.println("method=POST>");
out.println("<input type=hidden name=\"toBuy\" value=\"Vanilla\">");
out.println("<br>");
out.println("<input type=submit value=\"Vanilla\">");
out.println("</form>");
// a button for chocolate ice cream
out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("IceCreamGraphics"));
out.print("\" ");
out.println("method=POST>");
out.println("<input type=hidden name=\"toBuy\" value=\"Chocolate\">");
out.println("<br>");
out.println("<input type=submit value=\"Chocolate\">");
out.println("</form>");
// a button for strawberry ice cream
out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("IceCreamGraphics"));
out.print("\" ");
out.println("method=POST>");
out.println("<input type=hidden name=\"toBuy\" value=\"Strawberry\">");
out.println("<br>");
out.println("<input type=submit value=\"Strawberry\">");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
// the user submits one of the forms
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
//session = request.getSession(true);
kind = request.getParameter("toBuy");
// the rest is the same for POST and GET,
// so we let doGet() do the job
doGet(request, response);
}
}
The program also uses the file Order.java, see the first "ice cream
shop" example.
This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at emachkas@wellesley.edu
Spring Semester 2002