作者:blackwhites 日期:2000-11-13 17:50:11 以下是几种常调用的方法 Servlet to munication Listing 1: ServletBase public class ServletBase extends HttpServlet{ static Connection databaseConnection = null; public void init(ServletConfig _config) throws ServletException{ (_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( (襏SERNAME? ){ //- Display a message indicating they are already logged on }else{ logUserOn( (襏SERNAME? ); } } } 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(); ( ? 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 = ( ?; 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 = getServlet