rendering JSP to a string [duplicate]

2019-04-07 03:11发布

This question already has an answer here:

What is the way to render jsp file to a string? Is it possible without using frameworks like spring/struts/etc?..

标签: java jsp render
3条回答
看我几分像从前
2楼-- · 2019-04-07 03:51

You may compile JSP Pages with any JSP Rendering engine, for example Apache Jasper, included in Apache Tomcat.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-07 03:52

If you want to stream any webpage may it be a JSP or any other web page, you can use the method below.

import java.io.*;
import java.net.*;

public class c {

   public String getHTML(String urlToRead) {
      URL url;
      HttpURLConnection conn;
      BufferedReader rd;
      String line;
      String result = "";
      try {
         url = new URL(urlToRead);
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
            result += line;
         }
         rd.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }

   public static void main(String args[])
   {
     c c = new c();
     System.out.println(c.getHTML(args[0]));
   }
}

查看更多
孤傲高冷的网名
4楼-- · 2019-04-07 03:53

Use MockRunner. This allows you to run JSPs in test cases, for example.

查看更多
登录 后发表回答