JSP Scripts
JSP Scripts
Example
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>This JSP page uses page directives to set language, content type, and page encoding.</p>
</body>
</html>
```
In this example, the `page` directive sets the language to Java, the content type to `text/html`, and the
page encoding to `UTF-8`.
Include Directive
The include directive is used to include the content of another file at the time the JSP page is compiled.
Example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
```
In this example, the content of `header.jsp` and `footer.jsp` will be included at the top and bottom of
the page, respectively.
Taglib Directive
The taglib directive declares a tag library, which provides custom tags that can be used in the JSP.
Example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
```
In this example, the `taglib` directive declares the JSTL core tag library with the prefix `c`. The `<c:out>`
tag is used to output the value "Hello, World!".
Scriptlet
A scriptlet can contain any number of Java code statements that are executed every time the JSP is
requested.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Scriptlet Example</title>
</head>
<body>
<h1>Scriptlet Example</h1>
<%
%>
</body>
</html>
```
In this example, a scriptlet sets the `name` variable to "John Doe" and outputs a greeting.
Expression
An expression tag is used to insert the value of a Java expression directly into the output.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Expression Example</title>
</head>
<body>
<h1>Expression Example</h1>
</body>
</html>
```
In this example, the `<%= %>` syntax is used to insert the current date and time into the HTML.
Declaration
A declaration declares one or more variables or methods that can be used later in the JSP page.
Example:
%>
<!DOCTYPE html>
<html>
<head>
<title>Declaration Example</title>
</head>
<body>
<h1>Declaration Example</h1>
</body>
</html>
```
In this example, the `<%! %>` syntax is used to declare a method `getGreeting`, which is then used to
generate a greeting.