can a servlet read a jsp file and display its contents?

Hi, I would like to know if Servlets can read a Jsp file and display its contents.. Right now for our website, I am using a html file to be displayed after a successful post operation through the Servlets...-Thanks!
[243 byte] By [Gsunfun] at [2007-9-26 1:13:22]
# 1

i think it is not possible.

when a client request a servlet, all the output are interpreted by the client as html code, and then the browser display the content in a web page manner.

if u are trying to use out.println("<%jsp..something>")

i think it will not work since all the output has been flushed to the client and the browser doesnt know how to compile your jsp code (since jsp is compiled in run time--during the request)

hope this can help

cs_wwc at 2007-6-29 0:09:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
you could try opening a URL connection from servlet and getting jsp output that way
eugene_bendersky at 2007-6-29 0:09:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Your question is not very clear. What do u want to do exactly?
shubhrajit_c at 2007-6-29 0:09:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I posted this in another thread.

Here's some code that reads using a URL. This doesn't work with JSPs as the server executes JSP when it connects but it's useful for other file types. Note that the filename is a URI</h1>This is <code>show.jsp</code></h1>

<hr>

<%! String file = null; %>

<%! java.io.InputStream is = null; %>

<%

file = request.getParameterValues("filename")[0];

try {

is = (new java.net.URL(file)).openStream();

} catch (java.io.IOException ioe) { out.write(file + " not found!<br>"); }

java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(is));

String line = "";

%>

<pre>

<%

while((line = in.readLine()) != null) {

%>

<%=line%>

<%

}

%>

</pre>

eashanno at 2007-6-29 0:09:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thanks everyone for your reply..I believe even when we open a URLConnection to display the JSP contents, it can identify Only the HTML tags and not the jsp tags..

For shubhrajit_c :

the question I asked is: Whether a servlet can throw the output if the file which it reads is a JSP file instead of a HTML file. Normally, this is the scenario when you need to display a thank_you page after a successful order, the servlet is able to display the HTML page and not a JSP page.

Gsunfun at 2007-6-29 0:09:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

You need to get a "RequestDispatcher" for that JSP file and then "forward ()" the "ServletRequest" and "ServletResponse" to that JSP from your servlet. Processing is then handed over to that JSP file. Etc ...

Note: important words have been quoted. See references below for the people too lazy to look at the JDK docs or the Servlet SDK docs. Notice that "throw" clauses are not shown below.

ServletContext.getRequestDispatcher(java.lang.String path)

ServletContext.getNamedDispatcher(java.lang.String path)

ServletRequest.getRequestDispatcher(java.lang.String path)

RequestDispatcher.forward(ServletRequest request, ServletResponse response)

RequestDispatcher.include(ServletRequest request, ServletResponse response)

strrchr at 2007-6-29 0:09:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...