java实现bigpipe(下)

这里开始讲述后台部分的实现

 

 

<filter>
  		<filter-name>indexFilter</filter-name>
  		<filter-class>hrc.web.test.filter.IndexFilter</filter-class>
</filter>
<filter-mapping>
  		<filter-name>indexFilter</filter-name>
  		<url-pattern>/index.jsp</url-pattern>
</filter-mapping>

<servlet>
  		<servlet-name>PageletServlet</servlet-name>
  		<servlet-class>hrc.web.bigpipe.BigpipeServlet</servlet-class>
  		<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
  		<servlet-name>PageletServlet</servlet-name>
  		<url-pattern>/pageletServlet</url-pattern>
</servlet-mapping>
  	

这里的filter规定了那些页面需要使用bigpipe,servlet则是对具体的bigpipe进行具体的处理。在这个例子中,当用户访问index时,会先被IndexFilter处理。

 

 

public class IndexFilter extends AbstractBigpipeFilter {

	
	@Override
	protected List<PageletModel> getPageletModels(String path) {
		List<PageletModel> list=new ArrayList<PageletModel>();
		PageletModel model=new PageletModel();
		model.setPath("/pagelet1.jsp");
		model.setCss(path+"/css/page1.css");
		model.setJs(new String[]{path+"/js/page1.js"});
		model.setPid("p1");
		list.add(model);
		
		PageletModel model2=new PageletModel();
		model2.setPath("/pagelet2.jsp");
		model2.setCss(path+"/css/page2.css");
		model2.setJs(new String[]{path+"/js/page2.js"});
		model2.setPid("p2");
		list.add(model2);
		
		PageletModel model3=new PageletModel();
		model3.setPath("/pagelet3.jsp");
		model3.setCss(path+"/css/page3.css");
		model3.setJs(new String[]{path+"/js/page3.js"});
		model3.setPid("p3");
		list.add(model3);
		return list;
	}

}

 

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest) request;
		
		String url=req.getServletPath();
		
		logger.info(this.getClassNameAndMethod()+"url:"+url);
			
		req.setAttribute("page", url);
		req.setAttribute("list", getPageletModels(req.getContextPath()));
		req.getRequestDispatcher("/pageletServlet").forward(request, response); 
		
	}

 

 IndexFilter主要用来定义index.jsp需要用到的pagelet页面集合,每一个pagelet都设置了css、js、html、pid属性(这些属性用来干什么上一节有说)然后放进list中。它的父类AbstractBigpipeFilter得到这个list后,会把请求交给pageletServlet来处理。

 

BigpipeServlet

 

private void doPagelet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
	
		final PageletCount pageletCount = new PageletCount();	
		response.setContentType("text/html");
		response.setCharacterEncoding("gbk");
		
		String url=(String) request.getAttribute("page");
		RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
		dispatcher.include(request, response);
		response.getWriter().flush();      //先把主页面的内容全部输出出来
		
		List<PageletModel> list=(List<PageletModel>) request.getAttribute("list"); 
		for(PageletModel model:list){
			executor.execute(new Pagelet(pageletCount,request,response,model));  //pagelet逐个页面底部输出
		}
		
		synchronized (pageletCount) {
			try {
				pageletCount.wait();
			} catch (Exception e) {
				
			}
		}	
	}

 doGet、doPost会调用下列代码。它先将主页面(在这里例子中时index.jsp)的内容flush给浏览器,让用户可以先看到页面的大概样子。然后取得从AbstractBigpipeFilter中得到的pagelet消息,并通过ExecutorService把pagelet一个一个输出到页面底部。

 

synchronized (pageletCount) {
			try {
				pageletCount.wait();
			} catch (Exception e) {
				
			}
		}	

 最后的这句很关键。由于pagelet是多线程执行,如果执行这行,响应会在pagelet输出之前结束。

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java,Web)