-
1. Re: How can I call an JSP from an servlet and obtain the res
jonlee Jun 17, 2003 11:56 PM (in response to frodeh)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:
import org.apache.jasper.runtime.*;
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). -
2. Re: How can I call an JSP from an servlet and obtain the res
petertje Jun 19, 2003 3:31 PM (in response to frodeh)> 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()? -
3. Re: How can I call an JSP from an servlet and obtain the res
jonlee Jun 20, 2003 7:05 AM (in response to frodeh)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?
-
4. Re: How can I call an JSP from an servlet and obtain the res
petertje Jun 20, 2003 10:48 AM (in response to frodeh)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...
Cheers,
Peter. -
5. Re: How can I call an JSP from an servlet and obtain the res
jonlee Jun 20, 2003 11:21 PM (in response to frodeh)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.
-
6. Re: How can I call an JSP from an servlet and obtain the res
jpjjansen Jun 21, 2003 1:57 PM (in response to frodeh)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. -
7. Re: How can I call an JSP from an servlet and obtain the res
frodeh Oct 27, 2003 4:31 AM (in response to frodeh)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. -
8. Re: How can I call an JSP from an servlet and obtain the res
jonlee Oct 27, 2003 5:20 AM (in response to frodeh)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. -
9. Re: How can I call an JSP from an servlet and obtain the res
frodeh Oct 27, 2003 5:28 AM (in response to frodeh)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... -
10. Re: How can I call an JSP from an servlet and obtain the res
jonlee Oct 27, 2003 5:52 AM (in response to frodeh)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.
<virtual-host>localhost</virtual-host>