Klam
Klam

Reputation: 143

how to insert jsp from javacode into another jsp

i'm working on legacy code so please don't start "why are you doing it that way", I know it's terrible implementation so lets skip that.

In a high level I have a JSP

<html:form action="/myAction" method="POST" onsubmit="beforeSubmit()">
...
        <table class="dialog">
            <% render.myhtml(out); %>
        </table>
...
</html>

render.myhtml(out) is in the java code as follows (i tried with both jsp:include and @ include :

public void myhtml(Writer w) throws IOException {
....
        String include = "<jsp:include page=" +"\"" +myobject.getPage() +"\"" + " />";
      //String include = "<%@ include file=" +"\"" +myobject.getPage() +"\"" + " %>";
        println(w, include);
...
}

but when I open the page I cannot see the include.. the source code shows that it's printing the include tag but not evaluating it:

<table class="dialog">
    ...
    <jsp:include page="/path/test.jsp" />
</table>

So it seems to me that this insertion is happening AFTER the inclusions are evaluated.. so the question is.. what can I do to make this work? I've thought about reading the JSP as string and pass it but that's pretty heavy on memory so I want to avoid that.

Upvotes: 2

Views: 278

Answers (3)

Paul Grime
Paul Grime

Reputation: 15104

Use RequestDispatcher.include() in myhtml().

You will need to pass in application from your JSP, as a RequestDispatcher is accessed via ServletContext.getRequestDispatcher()

Application is a JSP implicit object: http://www.tutorialspoint.com/jsp/jsp_implicit_objects.htm

EDIT

JSP:

<html:form action="/myAction" method="POST" onsubmit="beforeSubmit()">
...
        <table class="dialog">
            <% render.myhtml(application, request, response); %>
        </table>
...
</html>

Java:

public void myhtml(ServletContext sc, ServletRequest req, ServletResponse res) throws IOException {
....
        RequestDispatcher rd = sc.getRequestDispatcher(myobject.getPage());
        rd.include(req, res);
...
}

I also like @Luiggi's answer, and you have a choice really depending on how you want to write your code.

Upvotes: 6

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

The explanation of the error is detailed in JB Nizet answer.

If you want to dynamically include a jsp using <jsp:include> you should just dynamically set the name of the page you want to include. Basic example:

<table class="dialog">
    <jsp:include page="<%= render.myhtml() %>" />
</table>

Having

public String myhtml() {
    return "/path/test.jsp";
}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

The writer that is passed to your render.myhtml() method is the HTTP response's writer. Everything you write to this writer goes to the browser, as is.

JSP is a server-side technology. It's compiled and executed at server-side. The browser doesn't know anything about JSP includes. So writing JSP code to the HTTP response writer doesn't make sense.

To do what you want to do, see Paul Grime's answer.

Upvotes: 1

Related Questions