tags:

views:

869

answers:

4

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

A: 

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]));
   }
}

Kalpak
A: 

Take a look at this question: Execute JSP directly from Java

ChssPly76
Thank you very much!
Overdose
A: 

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

Alexey Sviridov
A: 

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

Aaron Digulla