Problem with jsp:include Tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Problem with jsp:include Tag

    Code:
    <jsp:include flush="true" page="test_include.jsp?a=<%=java_variable%>&b=debasis&c=jana" />
    But the value of Java variable is not getting passed to the include page.
    If i pass plain hard coded value then it's getting passed.
    What should i be doing?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dmjpro
    Code:
    <jsp:include flush="true" page="test_include.jsp?a=<%=java_variable%>&b=debasis&c=jana" />
    But the value of Java variable is not getting passed to the include page.
    If i pass plain hard coded value then it's getting passed.
    What should i be doing?
    The several tags <% ... %> and <%= ... %> and <%@ ... %> and <%! ... %> don't nest properly in JSP; those tags don't change it a bit; imho that entire JSP syntax stinks. I find JSPs just a hack that got out of hand. You've just met one of its quircks.

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Actually this thing can be achieved by using <jsp:param name="param_nam e" value="<%=expre ssion%>"/>.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        Actually this thing can be achieved by using <jsp:param name="param_nam e" value="<%=expre ssion%>"/>.
        See? Another crappy solution because of that silly JSP syntax. Did you just find this hack?

        kind regards,

        Jos

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          include the java way

          There is a better solution. The jsp-tags aren't very flexible, and you can always do the same with pure Java. if you don't know how to do it in Java, just look at the corresponding java file that is the compiled jsp-file, placed in "work" directory of apache. There you can see how jasper translated your jsp-directives into java-code.

          My problem was similar to yours. Here is my solution:
          What for example if you want to have different menus on your webpages and the jsp-file for the menu (along with different parameters if needed) should be passed in runtime? Let's say I call "http://myServer/fullPage.jsp?to pMenu=myTop.jsp "

          here is fullPage.jsp:
          Code:
          <%!
          private boolean includeIfExisting(String pageName, PageContext pageContext)
          throws Exception
          {
          	HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
          	String url = request.getParameter(pageName);
          	if (url != null)
          	{
          		if (Log.isDebug()) Log.debug("before including " + pageName + " =" + url);
          		pageContext.include(url);
          		if (Log.isDebug()) Log.debug("after including " + pageName + " =" + url);
          
          		return true;
          	}
          	return false;
          }
          %>
          ...
          <div id=menu>
          <%
          	includeIfExisting("topMenu", pageContext);
          %>
          </div>
          ...

          Comment

          Working...