Hi,

When calling this servlet, I will get a java.lang.NullPointerException
with JSDK2.1 and his runner utility.
With JSDK2.0 and his servletrunner utility, it works fine.

The problem is, session.isNew() at the beginning of doPost(...) will
always return false.

When looking at your Servlet Specification Version 2.1, this must be
considered as a BUG.

The specification says:
Returns a Boolean value indicating whether this session is considered to
be new. A session is considered to be new if it has been created by the
server and not received from the client as part of this request. This
means that the client has not "acknowledged" or "joined" the session and
may not ever return the appropriate session identification information
when it makes its next request.

The code is from Servlet Essentials at
http://www.novocode.com/doc/servlet-essentials/.

<Snip>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShoppingCartServlet extends HttpServlet
{
  protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.print("<HTML><HEAD><TITLE>Online Shop</TITLE>"+
       "</HEAD><BODY><FORM METHOD=POST>"+
       "<INPUT TYPE=SUBMIT NAME=foo VALUE="+
       "\"Put a FOO into the shopping cart\">"+
       "<INPUT TYPE=SUBMIT NAME=bar VALUE="+
       "\"Put a BAR into the shopping cart\">"+
       "<INPUT TYPE=SUBMIT NAME=see VALUE="+
       "\"See the shopping cart contents\">"+
       "<INPUT TYPE=SUBMIT NAME=buy VALUE="+
       "\"Buy the shopping cart contents\">"+
       "</FORM></BODY></HTML>");
    out.close();
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)

            throws ServletException, IOException
  {
    String msg;

    HttpSession session = req.getSession(true);
    if(session.isNew())
    {
      session.putValue("foo", new int[] { 0 });
      session.putValue("bar", new int[] { 0 });
    }

    int[] foo = (int[])session.getValue("foo");
    int[] bar = (int[])session.getValue("bar");

    if(req.getParameter("foo") != null)
    {
      foo[0]++;
      msg = "Bought a FOO. You now have "+foo[0]+".";
    }
    else if(req.getParameter("bar") != null)
    {
      bar[0]++;
      msg = "Bought a BAR. You now have "+bar[0]+".";
    }
    else if(req.getParameter("buy") != null)
    {
      session.invalidate();
      msg = "Your order for "+foo[0]+" FOOs and "+bar[0]+
 " BARs has been accepted. Your shopping cart is empty now.";
    }
    else
    {
      msg = "You have "+foo[0]+" FOOs and "+bar[0]+
 " BARs in your shopping cart.";
    }

    res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");
    PrintWriter out = res.getWriter();
    out.print("<HTML><HEAD><TITLE>Shopping Cart</TITLE></HEAD><BODY>");
    out.print(msg);
    out.print("<HR><A HREF=\"");
    out.print(req.getRequestURI());
    out.print("\">Back to the shop</A></BODY></HTML>");
    out.close();
  }

  public String getServletInfo()
  {
    return "ShoppingCartServlet 1.0 by Stefan Zeiger";
  }
}
<Snap>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to