月关宝盒
以下是几种常调用的方法Servlet to Servlet CommunicationListing 1: ServletBasepublic class ServletBase extends HttpServlet{static Connection databaseConnection = null;public void init(ServletConfig _config) throwsServletException{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 Classpublic class logonServlet extends ServletBase{public void service(HttpServletRequest _req, HttpServletRe-sponse _res) throws ServletException{if ( isLoggedOn( _req.getParameter(襏SERNAME? ){//- Display a message indicating they are already logged on}else{logUserOn( _req.getParameter(襏SERNAME? );}}}Listing 3: Storing an Objectpublic class logonServlet extends HttpServlet{public void service(HttpServletRequest _req, HttpServletRe-sponse _res) throws ServletException{ServletContext thisContext = getServletContext();//-- Assume some method creates a new connection classConnection newConnection = createConnection();thisContext.setAttribute( database.connection? newConnection);//-- Return some output to the client}}Listing 4: retrieving an Objectpublic class logoffServlet extends HttpServlet{public void service(HttpServletRequest _req, HttpServletRe-sponse _res) throws ServletException{ServletContext thisContext = getServletContext();//-- Assume some method creates a new connection classConnection 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 Objectspublic class allServlet extends HttpServlet{public void service(HttpServletRequest _req, HttpServletRe-sponse _res) throws ServletException{ServletContext thisContext = getServletContext();//-- Assume some method creates a new Connection classEnumeration E = thisContext.getAttributeNames();while ( E.hasMoreElements() ){String name = (String)E.nextElement();System.out.println( "Object: " + name );}}}Listing 6: Retrieving Remote Contextspublic 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 classEnumeration E = otherContext.getAttributeNames();while ( E.hasMoreElements() ){String name = (String)E.nextElement();System.out.println( "Object: " + name );}}}Listing 7: Forwarding a Requestpublic 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 Contentpublic 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 ?);}}/////////////////////////////////////////forward方法是把请求的内容转发到另外的一个servlet.而include是把另一个servlet处理过后的内容拿过来.举例来说比如在servlet1打一句out.print("1111"),servlet2打上out.print("22222"),在servlet1中用forward命令会转到servlet2中,显示22222.而在servlet1中使用include方法会依然在servlet1的页面中,但是在1111后打出22222