How to include . jsp file in solution?

Asked

Viewed 1,056 times

3

I am developing Java web applications using JSP’s. I have a file called header.jsp, which contain links from CSS files and other things that are important to my system that is in the root directory.

I have a folder that has other files .jsp, but when I try to make an inclusion file header.jsp, doesn’t work.

  • Lucas, welcome to Stackoverflow in Portuguese, if you are Brazilian feel free to ask in Portuguese =)

  • 1

    @Lucasnascimento, what do you mean "when I try to make an inclusion file header.jsp, doesn’t work"? You’re not getting to include the .jsp in another?

  • I can do include if the file is in the same directory, but if it is in a different directory I cannot.

  • 1

    Without describing the problem in the question it will be very difficult for anyone to help you. Just saying it doesn’t work doesn’t give people enough information to help.

1 answer

4


So, let’s suppose you want to include the header.jsp inside a file called clientes.jsp (you did not specify the name, so I invented one to be able to exemplify). There are basically 4 ways to do this:


Alternative 1: include Directive

Just use the following on clientes.jsp:

<%@ include file="header.jsp" %>

That then will copy-and-paste the code of header.jsp in the clientes.jsp before compiling it for a java Servlet. Inclusion occurs at compile time.


Alternative 2: Standard action jsp:include

You use this way on clientes.jsp:

<jsp:include page="header.jsp" />

This then will make the compiled Servlet of header.jsp be invoked from within the compiled Servlet of clientes.jsp. Inclusion occurs at runtime when the request is being processed.

It is possible to pass parameters on jsp:include:

<jsp:include page="header.jsp">
    <jsp:param name="teste" value="Parâmetro de teste" />
</jsp:include>

In the archive header.jsp, you can take the value of the(s) parameter(s) using EL:

${param.teste}

Alternative 3: Tag c:import from JSTL

This other alternative is similar to the form used with the standard action:

<c:import url="header.jsp" />

The <c:import> allows you to import external documents into your application container:

<c:import url="http://example.com/header.jsp" />

And you can also use parameters:

<c:import url="header.jsp">
    <c:param name="teste" value="Parâmetro de teste" />
</c:import>

In the archive header.jsp, you can take the(s) value(es) of the(s) parameter(s) using EL in exactly the same way as with the standard action:

${param.teste}

Alternative 4: Tagfiles

This form is the most complicated, but it is also the most flexible, robust and powerful. You can put the file header.tag inside the briefcase WEB-INF/tags. Note that the file extension is tag and not jsp in this case.

So in your clientes.jsp just put it in the beginning:

<%@ taglib prefix="meuProjeto" tagdir="/WEB-INF/tags" %>

And down the middle, wherever you want to put the headline:

<meuProjeto:header/>

If you want to pass a (or more) parameter(s), use it like this:

<meuProjeto:header teste="Tagfiles são legais"/>

And there in your file header.tag, you put that in the beginning:

<%@ attribute name="teste" required="true" rtexprvalue="true" %>

And in the same file header.tag, you redeem the parameter(s) (s) with these extremely simple EL expressions:

${teste}

The required specifies whether the attribute is required or not (optional). The rtexprvalue indicates whether the value will be evaluated at runtime when the request is being processed or not.

Note that when using tagfiles, the result is similar to a function call, where you tell which parameters are required or not and the code of the clientes.jsp to include the header.tag It gets super dry and simple. In addition, the parameters are defined with a static (non-dynamic) scope, which makes it much easier for the developer to avoid problems of having to guess the parameter name or collide it with the name of something else and also forces an error if a required parameter is forgotten or if a non-existent parameter is used.

If the content of the parameter is too large, you can declare a body for it. To do this, put the following at the beginning of your header.tag:

<%@ tag body-content="tagdependent" %>

And to interpret the body:

<jsp:doBody/>

In the clientes.jsp you invoke this way:

<meuProjeto:header teste="tagfiles sao legais">
    Este é um exemplo de uma tagfile aonde é usado um corpo bem grande.
    Lorem ipssum dolor sit amet consectetuer adispiting elit.
</meuProjeto:header>

The body of the tag is interpreted when the <jsp:doBody> is invoked, it is not just copied and pasted, but is a form of a lambda in JSP. So, you can do this:

<meuProjeto:header teste="tagfiles sao legais">
    <c:if test="${variavelQualquer == 1}">
        O valor da variável é 1.
    </c:if>
    <c:if test="${variavelQualquer == 2}">
        O valor da variável é 2.
    </c:if>
</meuProjeto:header>

To finish, you can put your files tag within any subfolder of WEB-INF/tags, and usually just that is enough. Alternatively, it is possible to put them inside folders META-INF/tags that are inside the WEB-INF/libs, but in this case you will need to add a TLD file and it gets a little more complicated.


Source: "Head First Servlets & JSP" by Bryan Basham, Kathy Sierra and Bert Bates. I used the first edition, which is already old and outdated (of 2004), but still it is very good and highly recommend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.