Unfortunately, there is no universally accepted internal means of linking content from a JSP to a servlet whereas you can link servlet content to a JSP with the <jsp:include...> tag.
One of the problems is that JSPs do not exist until invoked and I don't believe that the spec covers an invocation from within the container. This makes it difficult to support porting. If you are only ever going to be using the Jetty/Tomcat containers then you can use the Jasper runtime call:
JspRuntimeLibrary.include(request, response, "MyJsp.jsp", out, true);
if you import the Jasper runtime library:
However, this is not portable. Your other option is to use the Apache httpclient library and invoke the JSP like any other client, through the HTTP interface (by making a HTTP client request and streaming the response back into your servlet).
> Unfortunately, there is no universally accepted
> internal means of linking content from a JSP to a
> servlet whereas you can link servlet content to a JSP
> with the <jsp:include...> tag.
What about javax.servlet.RequestDispatcher.include()?
I've got to say that I haven't tried this in a while. We were playing around with Tomcat trying to do this a while back, and we used to get messages like "OutputStream is already being used for this request" even if we hadn't done anything yet in the calling servlet. In the end, we gave it up. Has this been fixed now?
I don't know; haven't tried it either...
I was just wondering why you didn't mention it, as it _seems_ (or seemed? ;-) to be the standard way...
To be honest, I'd forgotten about that. I had a look in our lab notes though and found that we abandoned it for Tomcat. The call would work fine for servlets, but including static text or JSPs would generate the problem described. I guess we should eventually get around to retesting it.
If you can't get the JSPs to work, you might look into using Velocity templates. See http://jakarta.apache.org/velocity
You can easily implement it with a servlet. Those templates are web developer friendly.
We use velocity as a supplemental template engine
for our email templates. Works like a blast. It has a wide range of functionality, may be worth the try.
Well. From the discussion of the others here, I see that my real problem was not caught...
What I want to do is:
I have an administration-page, thar submits some content to a servlet.
This servlet is supposed to gather some content from a database, use a JSP to generate a HTML-page, insert this HTML into a new email, transmit the mail, and then return the process' result back to the admin-user that requested the mail-submission.
The key here, is that I want to use a JSP to generate the HTML, since this makes it easier to edit the page/mail in contrast to have all this code in the servlet. And I can reuse some og the JSP-elements I have created for use in the WebApp as well.
OK. Let's be clear here - you actually want the rendered HTML from the JSP not for the JSP to generate the e-mail with embedded HTML.
In which case, connect to the URL for the JSP, stream the information to a buffer and append it to your e-mail using the appropriate encoding for HTML-embedded e-mail (look at java.net.URL). You are actually better off using the Apache httpclient for this purpose as it caters for connection timeouts and so forth. However, it is beyond the capacity of the forum to answer specific questions on the creation of HTML-embedded e-mail - that is a rather large subject in itself.
This is almost what I want. This is what I do now, but I would like that the JSP would be invisible from outside..
Sort of 'in-house- connection directly to a compiled jsp/servlet inside JBoss, that is not mapped out in the WEB.xml
Sort of servlet/jsp for internal use only...
There are two ways - test the IP address of the requester:
String ipAddress = request.getRemoteAddr();
// If it matches some defined value keep going otherwise generate blank page
Or try setting the virtual host in the jboss-web.xml for your WAR in which the JSP resides - it will need to be in a separate WAR to your application.