Servlet generating jsp page

D

Dieter Salath?

Hi,

Is it possible to have a servlet which generates a jsp page, which
will then be compiled by the jsp compiler and executed?
We have a database which contains jsp pages, so it would be very easy
to load and construct them in the servlet, and after the servlet has
done the job, the jsp does the rest.

Code example:

public void doGet(...) {
// generate the jsp page
String jspPage = generateJspPage();

// write the jsp page to the output stream
response.getOutputStream().write(jspPage);
}

// now, the jsp page should be compiled and executed by the webserver
(Tomcat)

Any help is greatly appreciated

Dieter
 
D

David Rabinowitz

The webserver have this servlet, for example in Tomcat it is the
JasperServlet
 
B

Ben_

Your approach of "response.getOutputStream().write(jspPage)" is wrong
because it will feed the response with the source of the jsp and it won't be
compiled.

You have to create the JSP file on the file system, then either:
.. redirect the browser (response.sendRedirect method) to instruct it to
request the jsp in another request
.. include/forward to the jsp (RequestDispatcher.include or
RequestDispatcher.forward)
 
D

Dieter Salath?

Yes, but I can't use this servlet directly, because this servlet loads
the page from the file system. What I want to do is to generate the
jsp outside, dynamically, and then pass it to the JasperServlet.
 
G

Gyoergy Magoss

Hi,

Is it possible to have a servlet which generates a jsp page, which
will then be compiled by the jsp compiler and executed?
We have a database which contains jsp pages, so it would be very easy
to load and construct them in the servlet, and after the servlet has
done the job, the jsp does the rest.

Code example:

public void doGet(...) {
// generate the jsp page
String jspPage = generateJspPage();

// write the jsp page to the output stream
response.getOutputStream().write(jspPage);
}

this does not work (the explanation has been given in one of the
othere replies). You could however write the source to a file in the
directory serviced by your JSP-engine

String jspPage = generateJspPage() ;
// The following method should only give back the name beginning from
the
// document root directory of your JSP-Engine
String jspPageName = createAJSPNameForMe() ; //you must provide this
method
String jspPageFullName = getPhysicalRootDirectory() + jspPageName ;
FileWriter fw = new FileWriter(jspPageFullName) ;
fw.write(jspPage) ;
fw.close() ;
// Now redirect your request to jspPageName...


That should do it. However there is a drawback. In this case your JSP
is (usually) compiled every time you call it, because it is always
created anew and the system usually checks for the creation timestamp
and recompile it. Check if you can avoid that in your JSP-engine.
JSP-compilation takes an awful lot of time. It might be necessary to
implement a check in your servlet, if (a) this JSP has already been
created in the directory and (b) there is no newer version available
in your database. In this case you might skip the creating of the JSP
and forward directly to the JSP.
Secondly you must somehow find a way to synchronize the creation
otherwise you might and up with two requests parallel creating the JSP
and that might cause problems.

Regards

Gyoergy
 
B

Ben_

getPhysicalRootDirectory() + jspPageName ;
What about ServletContext.getRealPath(jspPageName) instead ?
Secondly you must somehow find a way to synchronize the creation
otherwise you might and up with two requests parallel creating the JSP
and that might cause problems.
You're right, this is usally underestimated.
 
D

Dieter Salath?

Hi again,

thank you very much for your help.
It works now.
The servlet generates the jsp, and the jsp engine does the rest. I
used the ServletContext.getRealPath(jspPageName) to create the jsp
File in the database.

I use the Jasper JSP engine, and the next problem now was, that the
jsp engine uses the scratchDir() method from the options field for
temporary files.
Now javac had a problem with my scratchDir, because it contains spaces
in the path! So I had to provide my own options class, with a
different scratchDir.
But after this modification, everything works now.

It's also very easy now to check whether the page had changed and to
create a new jsp page if necessary.

Thanks a lot

Dieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top