Scott Evans wrote:

> Craig and Kevin,
>
> First of all, thanks - I'm a newbie and I've really learned a lot from
> reading your correspondence.
>
> I'm dying to know, the instance of the action class, is it multithreaded or
> do you use it to create other instances of the same class?
>

In my implementation, the Action class implementation is required to be
multithreaded, because I create only one instance of each Action class (the first
time it is called).  Mostly, that just means avoiding instance variables in the
action classes -- IMHO bean classes you access within an action class should be
responsible for providing any required synchronization when possible race
conditions can occur.

>
> As well, what happens with input errors? More methods in the Action
> interface, like:
>

Again, this is my approach ... others will be different.  My "Action" interface
looks like this:

public interface Action {

    public void perform(HttpServlet servlet,
      HttpServletRequest request,
      HttpServletResponse response)
        throws IOException, ServletException;

}

The similarity to the calling signature for doGet() and doPost() is very
deliberate.  In essence, an Action class's perform() method can do anything that a
servlet's doGet() method can do.  For example, in the case of an input error, my
Action class does a RequestDispatcher.forward() back to the JSP page with the
input form on it.  I keep a session-scope bean with all of the input values that
the user last entered, which I use in the JSP page to show the page exactly as it
was input (plus an error message).

So, an action class might look like this:

public class SaveCustomerAction implements Action {

    // NOTE -- No instance variables because of multithreaded access!

    public void perform(HttpServlet servlet,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

        ... update the session bean with all the user's input values ...
        if (...there is an input error ...) {
            ... store an error message bean as a request attribute ...
            RequestDispatcher rd =

servlet.getServletContext().getRequestDispatcher("/enterCustomer.jsp");
            rd.forward(request, response);
            return;
        }
        ConnectionPool pool = getServletContext().getAttribute("pool");
        Connection conn = pool.allocateConnection();    // Connection pool is an
application scope bean
        ... save the customer in the database ...
        conn.close();    // Causes the connection to be returned to the pool
        RequestDispatcher rd =
          servlet.getServletContext().getRequestDispatcher("/nextMenu.jsp");    //
Or wherever
        rd.forward(request, response);
        return;

    }

}

As an aside, you can see above why I pass the servlet argument -- so that I can
access anything available through the servlet.  I can even log messages by calling
servlet.log(), or whatever else is necessary.


> thanks again,
>
> Scott
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to