Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP equivalent to the PHP include() function?

How should I include an HTML file into another HTML file, using JSP?

<jsp:include page="/include.html"></jsp:include>
like image 815
Dirk Diggler Avatar asked Jan 13 '10 01:01

Dirk Diggler


People also ask

What is the difference between JSP and PHP?

Mastering JSP requires knowledge of Java and HTML syntax, whereas PHP being a scripting language, is easier to learn and understand. JSP supports object caching with its extensive support for APIs, whereas PHP does not support caching. JSPs are very good at maintaining user sessions, whereas PHP destroys the user’s sessions every time.

What types of files can be included in a JSP file?

We can include the HTML, PHP, XML,JSP, etc it includes any files with different extensions that are already created or yet to be created in the web project. They include also one of the action tags like added the different sets of extension files into the current JSP file or page.

Can I use an expression in a JSP file?

Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file. The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression.

What is translation in JSP?

In the JSP life cycle, translation is one of the phases for the transformation of the codes process based on the user or project requirements. The include is one of the JSP directives for including the data contents related to any kind of resources with different extensions like JSP, HTML, or any text files.


2 Answers

You have a couple of options. The first is <jsp:include>. The second is <c:import>. The c: tags are JSTL, the JavaServer Pages Standard Tag Library.

What's the difference? Primarily <jsp:include> inserts the contents of another JSP page within the same JAR relative to the current page whereas <c:import> can read in an absolute or relative URL and display those contents on the page, retrieve a Reader or store the contents in a variable.

The syntax for both is XML-like so:

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

or

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

Note: both can take parameters.

like image 64
cletus Avatar answered Sep 28 '22 00:09

cletus


For those who want the same behavior as PHP include() or <!--#include file="header.jsp"-->, with shared the global scope in JSP, use the following command:

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

Reference: Here

like image 33
Roger Barreto Avatar answered Sep 28 '22 02:09

Roger Barreto