0% found this document useful (0 votes)
11 views5 pages

JSP Scripts

Enterprise Programming for java web development. all scripts in java scripts programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

JSP Scripts

Enterprise Programming for java web development. all scripts in java scripts programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Page Directives

Page directives provide instructions about the entire JSP page.

Example

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<!DOCTYPE html>

<html>

<head>

<title>Page Directives Example</title>

</head>

<body>

<h1>Page Directives Example</h1>

<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:

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

<!DOCTYPE html>
<html>

<head>

<title>Include Directive Example</title>

</head>

<body>

<h1>Include Directive Example</h1>

<p>This page includes a header using the include directive.</p>

</body>

</html>

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

```

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:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>

<html>

<head>

<title>Taglib Directive Example</title>

</head>

<body>

<h1>Taglib Directive Example</h1>


<c:out value="Hello, World!" />

</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:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<!DOCTYPE html>

<html>

<head>

<title>Scriptlet Example</title>

</head>

<body>

<h1>Scriptlet Example</h1>

<%

String name = "John Doe";

out.println("<p>Hello, " + name + "!</p>");

%>

</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:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<!DOCTYPE html>

<html>

<head>

<title>Expression Example</title>

</head>

<body>

<h1>Expression Example</h1>

<p>The current time is: <%= new java.util.Date() %></p>

</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:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>


<%!

private String getGreeting(String name) {

return "Hello, " + name + "!";

%>

<!DOCTYPE html>

<html>

<head>

<title>Declaration Example</title>

</head>

<body>

<h1>Declaration Example</h1>

<p><%= getGreeting("John Doe") %></p>

</body>

</html>

```

In this example, the `<%! %>` syntax is used to declare a method `getGreeting`, which is then used to
generate a greeting.

You might also like