HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Servlet to Servlet Communication"
Vol 5 Issue 5 p.106

	

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 " ); 
} 
} 


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.