10 Replies Latest reply on Oct 27, 2003 5:52 AM by jonlee

    How can I call an JSP from an servlet and obtain the result

      I want to run a timed servlet that connects to a database and uses a JSP to generate a HTML-text-object I can send by mail or ftp.

      I have created the timed servlet, and connectes periodically (3 mins) to the database, retrieves the info I need. I also manages to create a text-object to transmission, but I would like the functionallity of a JSP to create the text. Then a web-designer is able to create the 'look', without understanding the program-logic of the servlet..

      How ??

        • 1. Re: How can I call an JSP from an servlet and obtain the res
          jonlee

          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

            > 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

              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

                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

                  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

                    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

                      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

                        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

                          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

                            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>