java高并发-静态页面生成方案(2)

 
   

本章将实现一个servlet生成html的基本功能,包含了URL 重定向功能
 

     现在我们来看看一个普通的url分页请求怎么生成静态页面。
   
      假如现在要显示第一页的数据信息 ,那么通常的链接会是这个样子 : http://abc.com/ xx.do?pageNumber=1  。我们说一下这个最简单的方式,打个比方:当我们访问 http://abc.com/ xx_pageNumber_1.shtml   的时候 ,就是在访问 http://abc.com/ xx.do?pageNumber=1  。规律就是请求的 action(动作),和参数的名称,参数的值, 都用下滑线分开 ,而且请求的网页的最终后缀是 .shtm 。然后呢?我们需要编写一个servlet,去处理 .shtm 的请求: 

    

?
< servlet
< servlet-name >creatorHtmlServlet servlet-name >
    < servlet-class >com.jlins.CreatorHtmlServlet servlet-class >
    < load-on-startup >2 load-on-startup >
servlet >
< servlet-mapping >
   < servlet-name >creatorHtmlServlet servlet-name >
   < url-pattern >*.shtm url-pattern >
servlet-mapping >

     下面就是 servlet 的内容了: 

?
public class CreatorHtmlServlet extends HttpServlet {
 
      private Logger log=Logger.getLogger(CreatorHtmlServlet. class );
 
      public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
           doPost(request, response);
      }
 
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
           String encoding = "UTF-8" ;
           //获得请求address
           String templatePath = URLReWrite(request);
       //获得相对路径
           String realPath= request.getSession().getServletContext().getRealPath( "/" );
           //获得要生成的静态html文件的名字
           String htmlName = getHtmlFileName(request);
           //获得详细路径的 html文件的名字
           String cachhtmlFileName = realPath + File.separator + htmlName;
 
           File cacheFile = new File(cachhtmlFileName);
 
           boolean load = true ;
           //文件存在的话就可以直接返回了,不需要做任何处理
           if (cacheFile.exists()) {
              load = false ;
           }
           if (load) {
              final ByteArrayOutputStream os = new ByteArrayOutputStream();
              final ServletOutputStream stream = new ServletOutputStream() {  
 
                   public   void   write( byte [] data, int offset, int length)   {
                          os.write(data,   offset,   length);
                   }   
 
                   public   void   write( int   b)   throws   IOException   {
                          os.write(b);
                   }
               }; 
 
                final PrintWriter pw = new PrintWriter( new OutputStreamWriter(os, encoding));
 
                HttpServletResponse rep =  new HttpServletResponseWrapper(response)   {
 
                    public ServletOutputStream getOutputStream() {
                                   return stream;
                    }   
 
                    public PrintWriter getWriter() {
                                  return pw;
            }
                };
 
               logger.debug( "HtmlCreatorServlet RequestDispatcher = " + templatePath);
               //  使用 RequestDispatcher转发请求,请求真是的地址
               //  例如 index.shtm ,则转发到 index.do
               RequestDispatcher  dispatcher = getServletContext().getRequestDispatcher(templatePath);
               dispatcher.include(request, rep);
               pw.flush();
               FileOutputStream fos = null ;
               try {
 
                   if (os.size() == 0 ) {
                   // 验证一下用户转发的地址是否有效,无效的话就提示错误
                   response.sendError(HttpServletResponse.SC_NOT_FOUND, "" );
                   }
           else {
                   // servlet调用其他命令在相应的目录生成html文件,并且把文件返回给客户端
                   fos = new FileOutputStream(cachhtmlFileName);
                   os.writeTo(fos);
                   dispatcher = getServletContext(). getRequestDispatcher( "/" +htmlName);
                         dispatcher.include(request, response);
                   }
 
                   } finally {
                   if (fos != null ) {
                       fos.close();
                   }
              }
 
              } else {
                   RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/" +htmlName);                           dispatcher.include(request, response);
                }
             }
            // 主要的功能就是把http://abc.com/xx_pageNumber_1.shtm
            // 转换成 http://abc.com/xx.do?pageNumber=1  形式
            protected String URLReWrite(HttpServletRequest request) throws ServletException, IOException {
                String uri = request.getRequestURI();
                String contextPath = request.getContextPath();
                logger.debug( "HtmlCreator contextPath = " + contextPath);
                if (contextPath != null && contextPath.length() > 0 )
                    uri = uri.substring(contextPath.length());
 
                    uri = uri.substring( 0 , uri.length()- 5 );
 
                    String[] urls = uri.split( "_" );
 
                    uri = urls[ 0 ] + ".do" ;
                    if (urls.length > 1 ) {
                       for ( int i = 1 ; i < urls.length; i += 2 ) {
                       if (i== 1 ) {
                           uri += "?" + urls[i] + "=" + urls[i+ 1 ];
                       } else {
                         uri += "&" + urls[i] + "=" + urls[i+ 1 ];
                       }
                   }
                 }
 
              logger.debug( "HtmlCreatorServlet get uri = " + uri);
 
              return uri;
           }
            // 主要功能根据 http://abc.com/xx_pageNumber_1.shtm
            // 来得到即将要生成的html文件名字,也就是 xx_pageNumber_1.html
           private String getHtmlFileName(HttpServletRequest request) throws ServletException, IOException{   String uri = request.getRequestURI();
               String contextPath = request.getContextPath();
               if (contextPath != null && contextPath.length() > 0 )
               uri = uri.substring(contextPath.length());
 
               uri = uri.substring( 1 , uri.length()- 5 );
               uri += ".html" ;
               return uri;
           }

 上面就是整个代码了,非常的简单吧

本人原创文章@www.javady.com 转载请标明出处 http://www.javady.com/index.php/95.html


你可能感兴趣的:(java高并发-静态页面生成方案(2))