Logo of Bento frameworkspaceObject Oriented Programming


Source code example of controller Servlet


package example;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.bentofw.requestparser.*;
import com.bentofw.servletworker.*;
import com.bentofw.viewgenerator.*;
import com.bentofw.login.*;
import com.bentofw.util.*;

public class Example01 extends HttpServlet{

    private static ServletWorkerMap workerMap;
    private static ViewGenerator viewGen;
    private static RequestParser reqParser;
    private static LoginManager loginman;

    public void init()
        throws ServletException{
        try{
            workerMap=ServletWorkerMap.getInstance();
            workerMap.prepare();
            viewGen=ViewGenerator.getInstance();
	    loginman=LoginManager.getInstance();
            reqParser=RequestParser.getInstance();
        }
        catch(Exception ex){
            ex.printStackTrace();
            throw (new ServletException(ex));
        }
    }

    public void doPost(HttpServletRequest req,
                       HttpServletResponse res)
        throws ServletException,IOException{
        try{
            // Create the glue between MVC.
            ParsedRequestParameters params
               =reqParser.parseSimple(req);

            // The LoginManager identifies the user by the session
            // ID in the request path at the specified position.
            // It may throw:
            //     InvalidIdException
            //     AccessToHostDeniedException
            //     ExpiredIdException
            UserInfo uinfo=loginman.getInfoByPath(req,1);

            // By the XML for validation, ViewGenerator checks
            // if all the request parameters are valid or not.
            // The XML is selected by the request path and the
            // second parameter of the next method.
            VerifyResult result=viewGen.verify(params,0);
            if(!(result.isAllOk())){
               // The response is generated by the same XML as
               // the input validation.
               viewGen.writeResponseWithDefault(params,0,res,
                       result.getResultMap(),null,false);
               return;
            }

            // Picks the model for the request path and calls
            // its method.
            // The model is the implementation of ServletWorker
            // interface.
            // UserInfo is also passed to the model.
            WorkResult workResult
               =workerMap.work(req,params,uinfo);

            // Generate the View by the return value from the model.
            // The return value includes a set of String that will
            // be inserted into the XML. (the fifth parameter)
            // It also indicates which XML will be used to generate
            // the View. (the second parameter)
            viewGen.writeResponseWithDefault(
                    params,
                    workResult.getViewCode(),
                    res,
                    workResult.getValidityValues(),
                    workResult.getResponseValues(),
                    false);
            return;
	}
        catch(InvalidIdException ex){
            try{
                (req.getRequestDispatcher("invalidId.html")).
                   forward(req,res);
                return;
            }
            catch(Exception ex2){
                ex2.printStackTrace();
                throw (new ServletException(ex2));
            }
        }
        catch(AccessToHostDeniedException ex){
            try{
                (req.getRequestDispatcher("badHost.html")).
                   forward(req,res);
                return;
            }
            catch(Exception ex2){
                ex2.printStackTrace();
                throw (new ServletException(ex2));
            }
        }
        catch(ExpiredIdException ex){
            try{
                (req.getRequestDispatcher("expiredId.html")).
                   forward(req,res);
                return;
            }
            catch(Exception ex2){
                ex2.printStackTrace();
                throw (new ServletException(ex2));
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            throw (new ServletException(ex));
        }
    }

    public void doGet(HttpServletRequest req,
                      HttpServletResponse res)
        throws ServletException,IOException{
        ...
    }

}


Copyright © 1997-2007 OOP-Research CorporationTM, All Rights Reserved.