Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 java 生成html页面二种方法

java 生成html页面二种方法

发布时间:2018-09-08   编辑:www.jquerycn.cn
jquery中文网为您提供java 生成html页面二种方法等资源,欢迎您收藏本站,我们将为您提供最新的java 生成html页面二种方法资源

第一种方法相对很简单就是利用java去访问以request request, httpservletresponse response)
         throws servletexception, ioexception {
        string url = "";
        string name = "";

        servletcontext sc = getservletcontext();

        string file_name = request.getparameter("file_name");// 你要访问的jsp文件名,如index,不包括扩展名

        // 则你访问这个servlet时加参数.如http://localhost/test/tohtml?file_name=index

        url = "/" file_name ".jsf";// 你要生成的页面的文件名。我的扩展名为jsf .

        name = confconstants.context_path "" file_name ".htm";// 这是生成的html文件名,如index.htm.文件名字与源文件名相同。扩展名为htm

      //confconstants.context_path为你的应用的上下文路径。

        requestdispatcher rd = sc.getrequestdispatcher(url);

        final bytearrayoutputstream  = 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));

        httpservletresponse rep = new httpservletresponsewrapper(response) {
         public servletoutputstream getoutputstream() {
          return stream;
         }

         public printwriter getwriter() {
          return pw;
         }
        };
        rd.include(request, rep);
        pw.flush();
        fileoutputstream fos = new fileoutputstream(name); // 把jsp输出的内容写到xxx.htm
        os.writeto(fos);
        fos.close();
        printwriter ōut = response.getwriter();
        out
          .print("<p align=center><font size=3 color=red>页面已经成功生成!single<br>http://www.agilejava.org/space/? 233</font></p>");
       }
      }

</blockquote>

      第二步、配置你的web.xml

<blockquote>

 

       <servlet>
        <servlet-name>tohtml</servlet-name>
        <servlet-class>mj.util.html.tohtml</servlet-class>//你的servlet的类。
       </servlet>
       <servlet-mapping>
        <servlet-name>tohtml</servlet-name>
        <url-pattern>/tohtml</url-pattern>
       </servlet-mapping>

      第三步、运行servlet。如:http://localhost:8080/test/tohtml?file_name=index      

</blockquote>

      ok,这就在你的test项目的根目录下,生成了一个index.htm的静态文件。

您可能感兴趣的文章:
JSP 简介
教你如何成为一名Java初级程序员
PHP生成静态文件简单示例
Java 实例
JSP常见问题
Java 简介
php在jsp里面使用的么
php程序员面试题及答案(基础理论型)
php页面静态化的小例子
JSP 动作元素

[关闭]