• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

关于回答网友有关servlet和servlet之间通信的问题(建议放入精华区)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 28次 | 进入软件测试论坛讨论

领测软件测试网 以下是几种常调用的方法
     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(襏SERNAME? ){
     //- Display a message indicating they are already logged on
     }else{
     logUserOn( _req.getParameter(襏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();
     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 ?);
     }
     }

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网