File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Hitting a JSP from a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Hitting a JSP from a Servlet" Watch "Hitting a JSP from a Servlet" New topic

Hitting a JSP from a Servlet

Akram Reja

Joined: Mar 13, 2001
Posts: 17
I have a strange problem to solve. My servlet reads the source code for the JSP from the database, and writes it to a file, which the user can later hit. Before writing the JSP to a file, the servlet has to 'hit' the JSP, to make sure that the JSP gets compiled with no errors. sendRedirect and forward do not work in this scenario, because the control has to return to the servlet after 'hitting' the JSP. Can any one suggest me a soluation? I am basically looking for some sort of 'curl(a unix command)' that I can use in my servlet. Any suggestions?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
I can think of two possibilities:
1) dig into the Tomcat code and find out how to duplicate the effect of "hitting" the page by direct execution of the JSP page compiler.
2) address the page through a URLConnection to the localhost.
Akram Reja

Joined: Mar 13, 2001
Posts: 17
Thanks for your suggestions. I understand your first suggestion, but can you please elaborate on your second suggestion? Appreciate your help.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I think what Bill means is this. Open an URL connection and get the InputStream object and read the contents. I tested with a java application. You can include this code in servlet also.
When you run "java Test1" you will get

at the DOs command prompt.

maha anna

Akram Reja

Joined: Mar 13, 2001
Posts: 17
Thanks Maha. I didn't consider this possible at all. But, thanks to Bill and you, I have the solution.
I agree. Here's the link:
subject: Hitting a JSP from a Servlet
It's not a secret anymore!