List,

I'm having some trouble with using a RequestDispatcher, it works and doesn't work.

#doPost( req, res )

...
50 Customer customer =
51     Customer.findCustomer( emailAddress, custData);
52
53     if(( customer == null ) ||
54        ( password == null ) ||
55        ( !password.equals( customer.getPassword() ))) {
56        System.out.println( "one" );
57        gotoPage( "/move/accounts.jsp", request, response );
58        System.out.println( "four" );
59     }
60
61     customer.setStartDate( request.getParameter( "startDate" ) );
    ...
69     // branching code for other forwards
70     HttpSession session = request.getSession( true );
71
72     session.setAttribute( "customer", customer );
73     if( request.getParameter( "rooms" ) != null ) {
74         gotoPage( "/move/roomCount.jsp", request, response );
75     } else if( request.getParameter( "floors" ) != null ) {
76         gotoPage( "/move/floorCount.jsp", request, response );
77     } ...

#gotoPage( url, req, res )

private void gotoPage( String address,
                       HttpServletRequest request,
                       HttpServletResponse response )
        throws ServletException, IOException {
    RequestDispatcher dispatcher =
        getServletContext().getRequestDispatcher( address );
    System.out.println( "two" );
    dispatcher.forward( request, response );
    System.out.println( "three" );
}

Basically, the idea is if the user forgets to enter a username or password, the servlet should forward--via gotoPage( url, req, res )-- to accounts.jsp. This blows up with a NPE at line 61 everytime (the lookup for the customer returns null, so the NPE is expected). Looking at standard out I see:

one
two
three
four

This shows that the test for null passed and the call to gotoPage worked as expected but passed through and continued to process the page, null reference be damned.

Yet, if the user correctly completes the form, all is fine and the dispatcher created in gotoPage works as expected. Standard out looks like:

one
two


What am I doing wrong that the dispatcher does not like in the first instance? "/travel/accounts.jsp" is available.


TIA,
Tim

PS: for those paying attention this code is loosely based on work by Marty Hall in /More Servlets and JavaServer Pages/


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to