0
votes

How can i include a JSP page in another JSP with the IF structure:

Can i use this part of code in a JSP file:

<%
        UtilisateurAction useraction = new UtilisateurAction();
        String statut = useraction.Connect();
        //System.out.println(statut);

        if(statut=="ADMIN"){
      %>    <%@ include file="menu.jsp"%>

       <%} 

        else {if(statut=="USER"){%>
            <%@ include file="menu.jsp"%>


        <%}} %> 
4
yes, i believe you canOlayinka
yes, but generally java code in a jsp page is bad :) what about <c:if>? P.s and maybe you want to use "ADMIN".equals()Marco Acierno
okey but how can use <c:if> ??Salima Lifri
when i try to use <c:if> i have an error "unknown tag", i already add the jstl and standard library !!!!!!! PLZ helpSalima Lifri

4 Answers

1
votes

If all you want is the possibility to use <c:if> you must have a scoped variable. Attributes of current request, session of application context are with respective scope being request, session and application.

You can easily put variables in page context (valid only for the current jsp, and others included by @include but not by <jsp:include>) :

<%
...
String statut = useraction.Connect();
pageContext.setAttribute("statut", statut);
%>
<c:if test="${statut == 'ADMIN'}">
...
</c:if>

But if you want to to many excluvise test the correct tag is <c:choose> :

<%
String statut = useraction.Connect();
pageContext.setAttribute("statut", statut);
%>
<c:choose>
    <c:when test="${statut eq 'ADMIN'}">
        ...
    </c:when>
    <c:when test="${statut == 'USER'}">
        ...
    </c:when>
    <c:otherwise>
        <p>Status ${statut} not found</p>
    </c:otherwise>
</c:choose>

I've just tested it it works (on my machine ...), and the otherwise clause could explain why you do not get what you want.

1
votes

The <%@ include file="menu.jsp"%> is static include, so it will include contents of the menu.jsp no matter of the condition. But it will be executed only if the condition is true.

The <jsp:include page="menu.jsp" flush="true"/> is dynamic include, so, the menu.jsp page will actually be called only if the condition is satisfied. But from the other side menu.jsp must be a full page, not a page fragment, and cannot use variables defined in the 'parent' page.

So, it really depends on what you want to achieve.

And as Marco suggested, you should use "ADMIN".equals(statut) instead of ==.

1
votes

You can use <jsp:include page="menu.jsp" flush="true"/> to include pages in jsp, on conditions checks.

If you are not using jstl and trying to include menu.jsp when statut value is either ADMIN or USER, you can do it like:

<% if (statut.equals("ADMIN") || statut.equals("USER")) { %>
          <jsp:include page="menu.jsp" flush="true"/>
<% }%>

Note that menu.jsp should be in the same folder of your current jsp, else give relative path.

0
votes

so i must use the . what i want is to include different pages depending on the statut value.

<%
    UtilisateurAction useraction = new UtilisateurAction();
    String statut = useraction.Connect();
    //System.out.println(statut);

    if(statut=="ADMIN"){
  %>    <%@ include file="menu.jsp"%>

   <%} 

    else {if(statut=="USER"){%>
        <%@ include file="menuother.jsp"%>


    <%}} %>