KEMBAR78
Java Server Page1 STD | PDF | Software Engineering | Computer Programming
0% found this document useful (0 votes)
21 views8 pages

Java Server Page1 STD

Java Server Pages (JSP) is a technology used to create dynamic web pages, allowing interaction with databases and server-side processing. JSP simplifies the development of dynamic content compared to servlets by embedding Java code directly into HTML. Key components of JSP include directives for configuration, scripting elements for embedding Java code, and the ability to handle dynamic data effectively.

Uploaded by

Muktar Fakir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Java Server Page1 STD

Java Server Pages (JSP) is a technology used to create dynamic web pages, allowing interaction with databases and server-side processing. JSP simplifies the development of dynamic content compared to servlets by embedding Java code directly into HTML. Key components of JSP include directives for configuration, scripting elements for embedding Java code, and the ability to handle dynamic data effectively.

Uploaded by

Muktar Fakir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Server Pages

JSP
What is JSP?
JSP is Java Server Page, which is a dynamic web page and used to build dynamic websites. To run jsp,
we need web server which can be tomcat provided by apache, it can also be jRun, jBoss(Redhat),
weblogic (BEA) , or websphere(IBM).

What is the need of JSP when there is a servlet

Client ( Browser ) Server


Login.html request

NAM
Process

Simply is a login dot html page

Login.html = Login dot html is static content

out.print(“<html>”);
out.print(“<html>”);
out.print(“<html>”);
out.print(“<html>”);
Responce out.print(“<html>”);
out.print(“<html>”);

JSP came after all the servlet because it was very difficult to make designing dynamic part through all the servlet.
JSP was introduced to make comic web pages easier

JSP (JavaServer Pages) vs HTML

 HTML (HyperText Markup Language) is a static file that contains fixed content. It cannot
interact with databases or display dynamic content. It is simply used to structure and display
the content on web pages.

 JSP (JavaServer Pages) is a dynamic web file that allows interaction with databases and can
display dynamic data. It is more powerful as it can be programmed and controlled by the
developer. It is typically used for building web applications that need to handle dynamic
content.

 File Extension: JSP files are saved with the .jsp extension, while HTML files are saved
with .html.
 Compilation: When a JSP file is accessed, it is first converted into a Java servlet by the server.
This conversion usually happens the first time the JSP is requested.

 Interactivity: JSP can communicate with databases (e.g., MySQL) and handle dynamic data
by embedding Java code directly into the page. This makes it suitable for building
applications that require server-side processing.

In summary, while HTML is static and cannot interact with a database, JSP allows for dynamic content
and server-side interaction, making it a powerful tool for web development.
JSP Elements
A JSP (JavaServer Pages) file is made up of three important types of elements that help in building dynamic
content:
1. Directives:
o Directives are special instructions in a JSP that provide information to the JSP container (the
server program executing the JSP).
o They do not produce any output but configure aspects of the JSP page such as its behavior,
page settings, or content type.
o A directive is typically written at the beginning of the JSP file and is enclosed within <%@ %>
tags.
o Example
o <%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>

Scripting Elements:
 Scripting elements allow programmers to embed Java code directly into the JSP page.
 This Java code is converted into the servlet when the JSP is compiled, and the code becomes part of
the servlet’s logic.
 There are three types of scripting elements:
o Declarations: Declares variables or methods that will be available in the servlet.

<%! int counter = 0; %>


o Scriptlets: Contains Java code that is executed when the page is requested.
<% counter++; out.println("Count: " + counter); %>

o Expressions: Evaluates an expression and inserts its value into the output.
<%= "Hello, World!" %>

JSP Directives
Directives in JSP are special instructions that communicate with the JSP container (the server
program executing the JSP). They allow the programmer to configure the settings of the JSP
page, include content from other resources, and specify custom tag libraries for more
complex functionality.
Syntax of Directives:
jsp
CopyEdit
<%@ directive_name attribute1="value1" attribute2="value2" ... %>
Types of Directives:
1. Page Directive:
o The page directive defines global settings for the JSP page, which are
processed by the JSP container.
o Syntax:
jsp
CopyEdit
<%@ page attribute1="value1" attribute2="value2" ... %>
o Attributes in the Page Directive:
 language: Specifies the programming language used in the JSP (usually
Java).
jsp
CopyEdit
<%@ page language="java" %>
 contentType: Defines the content type (e.g., HTML or XML) and the
character encoding (e.g., UTF-8).
jsp
CopyEdit
<%@ page contentType="text/html; charset=UTF-8" %>
 import: Imports Java classes that can be used in the JSP.
jsp
CopyEdit
<%@ page import="java.util.*, javax.servlet.*" %>
 session: Specifies whether the page participates in HTTP sessions. It
can be set to true or false.
jsp
CopyEdit
<%@ page session="true" %>
 buffer: Defines the size of the buffer for the output.
jsp
CopyEdit
<%@ page buffer="8kb" %>
 errorPage: Specifies a JSP page that handles errors for the current
page.
jsp
CopyEdit
<%@ page errorPage="error.jsp" %>
2. Include Directive:
o The include directive is used to include the content of another resource
(either static or dynamic) into the JSP during translation time.
o This inclusion happens before the page is sent to the client (i.e., during the
server-side processing phase).
o Syntax:
jsp
CopyEdit
<%@ include file="filename.jsp" %>
o Example:
jsp
CopyEdit
<%@ include file="header.jsp" %>
This will insert the contents of header.jsp at the point where this directive appears.
3. Taglib Directive:
o The taglib directive is used to declare a tag library in the JSP. This allows you
to use custom tags defined in a tag library, which can simplify coding and
encapsulate complex functionality.
o Syntax:
jsp
CopyEdit
<%@ taglib uri="tagLibraryURI" prefix="tagPrefix" %>
o Example:
jsp
CopyEdit
<%@ taglib uri="http://example.com/tags" prefix="mytags" %>
This imports a custom tag library from the specified URI, and you can now use the tags with
the specified prefix (e.g., <mytags:customTag>).
Page Directive:
 The page directive is used to set various global settings for the entire JSP page.
 You can use multiple page directives in a JSP file, but each attribute can only be
specified once in a page directive.
 Some of the common attributes for the page directive are:
o language: Specifies the language used in the JSP (typically java).
o import: Imports Java classes that will be used in the JSP.
o contentType: Sets the content type (e.g., text/html) and character encoding.
o session: Specifies whether the page participates in an HTTP session.
Example of Page Directive:
jsp
CopyEdit
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*,
javax.servlet.*" session="true" %>
Summary:
 Directives are used to provide global settings and configure the behavior of a JSP
page.
 The page directive controls the overall settings for the JSP.
 The include directive helps in including content from other resources.
 The taglib directive makes custom tag libraries available for use in the JSP page.
These directives allow developers to make their JSP pages more flexible, efficient, and
manageable.

JSP Page Directive Syntax Explained


The page directive is used to define various settings for a JSP page. It helps the programmer configure how the
page will be handled by the JSP container (the server).
Here's a breakdown of the page directive attributes:

Syntax:-
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true|false" ]
[ buffer="none|8kb|sizekb" ]
[ autoFlush="true|false" ]
[ isThreadSafe="true|false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ; charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]
[ isErrorPage="true|false" ]
[ pageEncoding="characterSet | ISO-8859-1" ] %>

Attributes Explained:
1. language="java":
o Specifies the programming language used in the JSP (default is java).
o Example: <%@ page language="java" %>
2. extends="package.class":
o Allows the JSP to extend a Java class.
o Not commonly used. Typically, you'd write a JSP that extends a servlet or
other classes.
3. import="package.class | package.*":
o Imports Java classes or entire packages to be used in the JSP.
o Example: <%@ page import="java.util.*, javax.servlet.*" %>
4. session="true|false":
o Defines whether the page participates in the HTTP session.
o true: The page will use the session (default).
o false: The page won't use the session.
o Example: <%@ page session="false" %>
5. buffer="none|8kb|sizekb":
o Sets the buffer size for the output.
o none: No buffer.
o 8kb: 8KB buffer (default).
o Example: <%@ page buffer="8kb" %>
6. autoFlush="true|false":
o If set to true, the buffer will automatically flush when it reaches the specified
size.
o false means you manually flush the buffer.
o Example: <%@ page autoFlush="true" %>
7. isThreadSafe="true|false":
o true means the page is thread-safe (can handle multiple requests
simultaneously).
o false means the page is not thread-safe.
o Example: <%@ page isThreadSafe="true" %>
8. info="text":
o Provides a description of the page, often used for debugging purposes.
o Example: <%@ page info="This is the homepage" %>
9. errorPage="relativeURL":
o Specifies a JSP page that will handle errors for the current page.
o Example: <%@ page errorPage="error.jsp" %>
10. contentType="mimeType [ ; charset=characterSet ]":
o Sets the MIME type and optionally the character set for the page.
o Example: <%@ page contentType="text/html; charset=UTF-8" %>
11. isErrorPage="true|false":
o If set to true, the page will act as an error page (handles exceptions).
o Example: <%@ page isErrorPage="true" %>
12. pageEncoding="characterSet | ISO-8859-1":
o Specifies the character encoding used for the page.
o Example: <%@ page pageEncoding="UTF-8" %>

Summary:
 Directives in JSP allow you to configure settings like language, session management,
buffer size, MIME type, error handling, and more.
 The page directive is used to define global settings for how the JSP should behave on
the server.
Each of these attributes helps control the functionality, performance, and behavior of the
JSP, making it a powerful tool for building dynamic web pages.

Scripting Elements: Declarations in JSP

In JSP, scripting elements allow you to embed Java code directly into the HTML content. One type of
scripting element is declarations.

Declarations:

 Purpose: A declaration is used to declare variables or methods that will be used in the JSP
later on.

 Scope: These declared variables and methods are available throughout the JSP file, so you
can use them anywhere in your JSP code (within scriptlets or expressions).

 Syntax: Declarations are written inside <%! %> tags.

Syntax Example:

jsp

CopyEdit

<%! int counter = 0; %>

<%! public int incrementCounter() { return ++counter; } %>

Explanation:

1. Declare Variables:

o You can declare variables that will hold data to be used later.

o Example: int counter = 0; declares an integer variable counter with an initial value of
0.
2. Declare Methods:

o You can declare methods to perform actions or computations.

o Example: public int incrementCounter() { return ++counter; } declares a method


incrementCounter() that increases the value of counter by 1 each time it's called.

Where to Use Declarations:

 Declarations are useful when you need to:

o Declare global variables to be accessed across the JSP page.

o Create helper methods to perform calculations or other operations.

Key Points:

 Declarations are placed at the top of the JSP page (typically before any other scripting
elements).

 Once declared, the variables and methods can be used in scriptlets or expressions within the
JSP.

Example of Using Declarations:

jsp

CopyEdit

<%! int counter = 0; %>

<%! public int incrementCounter() { return ++counter; } %>

<html>

<body>

<h1>Counter: <%= incrementCounter() %></h1>

</body>

</html>

 In this example:

o counter is declared at the top of the page.

o incrementCounter() is called in the expression <%= %> to display the updated


counter value.

Summary:

 Declarations are used to declare variables or methods that can be used throughout the JSP
page.

 They are written inside <%! %> tags and provide global variables and methods for use in your
JSP code.
This makes the JSP more dynamic by enabling reusable logic across different parts of the page.

Syntax
<%! Java declaration statements %> //This syntax allows you to declare one or more Java variables or
methods.
Example,
<%! private int count = 0; %>
<%! int i = 0; %>

Explanation of the Example:


1. <%! private int count = 0; %>
o This declares a variable count of type int and initializes it to 0.
o The private keyword ensures that this variable is accessible only within the JSP file.
2. <%! int i = 0; %>
o This declares another variable i of type int and initializes it to 0.
Where to Use Declarations:
 Declarations are placed at the top of the JSP page, before any HTML or other scripting
elements. This makes the declared variables and methods available throughout the page.
 You can use the declared variables and methods later in the page inside scriptlets (<% %>) or
expressions (<%= %>).

You might also like