Is there a good way to wrap a jsp in a jsp?

5 views
Skip to first unread message

e.d.pro...@gmail.com

unread,
Mar 17, 2022, 4:20:43 PMMar 17
to
We have a jsp landing page that has a menu and a horizontal line.
Under the horizontal line we want to display the selected menu option which is another jsp, without pasting the main page code into the other file.
The jsp is initially called from a @WebServlet using request.getRequestDispatcher().forward(), and I want to pass variables into the nested jsp.
I use request.setAttribute(), or session.setAttribute() to set variables, and they resolve in the main page jsp but not the nested jsp with any method I could find so far to dynamically display the nested page.

Arne Vajhøj

unread,
Mar 17, 2022, 7:52:35 PMMar 17
to
A few ideas:

1) Just (mis)use session to pass the info.

2) Use dynamic include instead of static include and pass
info as query parameter.

3) Switch to a master page capable technology like
JSF facelets - I seem to recall that there exist
a Java web framework that supports master pages via
a taglib, but I cannot remember the name.

Arne

e.d.pro...@gmail.com

unread,
Mar 18, 2022, 12:19:20 PMMar 18
to

> A few ideas:
>
> 1) Just (mis)use session to pass the info.
>
> 2) Use dynamic include instead of static include and pass
> info as query parameter.
>
> 3) Switch to a master page capable technology like
> JSF facelets - I seem to recall that there exist
> a Java web framework that supports master pages via
> a taglib, but I cannot remember the name.
>
> Arne

I've never used Facelets, from what I can tell that may be a better architecture for that sort of thing but would require a full rewrite.
I couldn't figure a solution that passes everything as is, where you can reference ${var} in the parent JSP and ${var} in the nested JSP, but I did find this option seems to work:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:import url="${pagetoload}">
<c:param name="passvar" value="${vartopass}"/>
</c:import>

Then in the nested JSP, have to reference variables like ${param.passvar}

e.d.pro...@gmail.com

unread,
Mar 23, 2022, 2:41:05 PMMar 23
to
> >
> > Arne
> I've never used Facelets, from what I can tell that may be a better architecture for that sort of thing but would require a full rewrite.
> I couldn't figure a solution that passes everything as is, where you can reference ${var} in the parent JSP and ${var} in the nested JSP, but I did find this option seems to work:
> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
> <c:import url="${pagetoload}">
> <c:param name="passvar" value="${vartopass}"/>
> </c:import>
>
> Then in the nested JSP, have to reference variables like ${param.passvar}
I found that you can pass values one at a time from one page file to another using import params, but if the page does something, like call a form .submit(), those values get lost.
So to set up constants available to every page as ${variable}, I use request.getServletContext().setAttribute() in a javax.servlet.Filter, which can be in a common module referenced from multiple war modules.
Reply all
Reply to author
Forward
0 new messages