Wednesday, May 4, 2011

ServletBase


     Servlet to Servlet Communication
 
     Listing 1: ServletBase
     public class ServletBase extends HttpServlet{
     static Connection databaseConnection = null;
     public void init(ServletConfig _config) throws ServletException{
     super.init(_config);
     if ( databaseConnection == null )
     //- Open up the database connection
     }
     protected boolean isLoggedOn( String _username ){
     return true;
     }
     protected boolean logUserOn( String _username ){
     return true;
     }
     }
     Listing 2: Using the NewSerletBase Class
     public class logonServlet extends ServletBase{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     if ( isLoggedOn( _req.getParameter(ÒUSERNAMEÓ) ){
     //- Display a message indicating they are already logged on
     }else{
     logUserOn( _req.getParameter(ÒUSERNAMEÓ) );
     }
     }
     }
     Listing 3: Storing an Object
     public class logonServlet extends HttpServlet{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     ServletContext thisContext = getServletContext();
     //-- Assume some method creates a new connection class
     Connection newConnection = createConnection();
     thisContext.setAttribute( Òdatabase.connectionÓ, newConnection );
     //-- Return some output to the client
     }
     }
     Listing 4: retrieving an Object
     public class logoffServlet extends HttpServlet{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     ServletContext thisContext = getServletContext();
     //-- Assume some method creates a new connection class
     Connection newConnection = thisContext.getAttribute(
     Òdatabase.connectionÓ);
     if ( newConnection == null )
     //- Database has not been opened yet
     //-- Return some output to the client
     }
     }
     Listing 5: Looking at All the Objects
     public class allServlet extends HttpServlet{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     ServletContext thisContext = getServletContext();
     //-- Assume some method creates a new Connection class
     Enumeration E = thisContext.getAttributeNames();
     while ( E.hasMoreElements() ){
     String name = (String)E.nextElement();
     System.out.println( "Object: " + name );
     }
     }
     }
     Listing 6: Retrieving Remote Contexts
     public class otherServlet extends HttpServlet{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     ServletContext otherContext =
     getServletContext(Òhttp://<otherdomain>/servlet/allServletÓ);
     //-- Assume some method creates a new Connection class
     Enumeration E = otherContext.getAttributeNames();
     while ( E.hasMoreElements() ){
     String name = (String)E.nextElement();
     System.out.println( "Object: " + name );
     }
     }
     }
     Listing 7: Forwarding a Request
     public class forwardServlet extends HttpServlet{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     ServletContext xt = getServletContext();
     RequestDispatcher xyzServlet =
     xt.getRequestDispatcher(Òhttp://<domain>/servlet/xyzServletÓ);
     //- Do any preliminary processing
     _req.setAttribute( Òdatabase.resultsÓ, new Results() );
     xyzServlet.forward( _req, _res );
     }
     }
     Listing 8: Inserting Content
     public class insertServlet extends HttpServlet{
     public void service(HttpServletRequest _req, HttpServletRe-
     sponse _res) throws ServletException{
     ServletContext xt = getServletContext();
     RequestDispatcher xyzServlet =
     xt.getRequestDispatcher(Òhttp://<domain>/servlet/xyzServletÓ);
     PrintWriter Out = _res.getWriter();
     Out.println( ÒThis is from the insertServlet Ò );
     for(int x=0; x < 10; x++ )
     xyzServlet.insert( _req, _res );
     Out.println( ÒThis is the end of the print servlet Ò );
     }
     }

No comments:

Post a Comment