Shunmugavel Krishnan
Shunmugavel Krishnan

Reputation: 787

How to include a HTML in JSP at runtime?

I need to include html or image in a JSP at runtime. I will come to know about the jsp filename at the time of runtime. So I can't able to make the change for a JSP include.

How can I do this?

Upvotes: 2

Views: 1731

Answers (1)

Kaur Kase
Kaur Kase

Reputation: 126

I am not sure what you mean by runtime? I have done something maybe similar. In the controller I do. model.addAttribute("jspContent", "test.jsp")

And then in the containing jsp file :

<jsp:include page="${jspContent}" ></jsp:include>

Edit:
Read your comment. I guess then it depends on what other technologies you are using. You could add the name to be included to the session and then read it in the Controller that is receiving the redirect. Adding it to the model and clearing from session. Or if you happen to use Spring I just yesterday got to know about this: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes

Edit2: I meant something like this, I am not entirely sure if it's suitable, but it's an idea :) In the controller that is redirecting you do something like this:

session.setAttribute("jspContentFromRedirect", "test.jsp");
servletResponse.sendRedirect(urlToRedirectTo);

And then in the receiving Controller:

String jspContent = session.getAttribute("jspContentFromRedirect");
if(jspContent != null){
    model.addAttribute("jspContent", jspContent);
    session.setAttribute("jspContentFromRedirect", null);
    }

Something like this

Upvotes: 2

Related Questions