7/27/2019 Java Jsp Presentation
1/24
Softsmith Infotech 1
JSP
Java Server Pages
7/27/2019 Java Jsp Presentation
2/24
Softsmith Infotech 2
Java Server Pages
JSP (Java Server Pages) is an alternate way of creating servlets
JSP is written as ordinary HTML, with a little Java mixed in
The Java is enclosed in special tags, such as
The HTML is known as the template text
JSP files must have the extension .jsp
JSP is translatedinto a Java servlet, which is then compiled
Servlets are run in the usual way
The browser or other client sees only the resultant HTML, asusual
Tomcat knows how to handle servlets and JSP pages
7/27/2019 Java Jsp Presentation
3/24
Softsmith Infotech 3
Jsp Execution
7/27/2019 Java Jsp Presentation
4/24
Softsmith Infotech 4
JSP scripting elements
There is more than one type of JSP tag, depending on what you want
done with the Java
The expression is evaluated and the result is inserted into the HTML
page
The code is inserted into the servlet's service method
This construction is called a scriptlet
The declarat ions are inserted into the servlet class, not into a method
7/27/2019 Java Jsp Presentation
5/24
Softsmith Infotech 5
Example JSP
Hello! The time is now
Notes:
The tag is used, because we are computing a value
and inserting it into the HTML
The fully qualified name (java.util.Date) is used, instead of the
short name (Date), because we havent yet talked about how to
do import declarations
7/27/2019 Java Jsp Presentation
6/24
Softsmith Infotech 6
Variables
You can declare your own variables, as usual
JSP provides several predefined variables
request : The HttpServletRequest parameter
response : The HttpServletResponse parameter
session : The HttpSession associated with the request, ornull ifthere is none
out : A JspWriter (like a PrintWriter) used to send output to the
client
Example:
Your hostname:
7/27/2019 Java Jsp Presentation
7/24
Softsmith Infotech 7
Scriptlets
Scriptlets are enclosed in tags
Scriptlets do notproduce a value that is inserted directly into theHTML (as is done with )
Scriptlets are Java code that maywrite into the HTML
Example:
Scriptlets are inserted into the servlet exactly as written, and are notcompiled until the entire servlet is compiled
Example:
Have a nice day!
Have a lousy day!
7/27/2019 Java Jsp Presentation
8/24
Softsmith Infotech 8
Declarations
Use for declarations to be added to your servlet class, not
to any particular method
Caution: Servlets are multithreaded, so nonlocal variables must
be handled with extreme care
If declared with, variables are local and OK Data can also safely be put in the request orsession objects
Example:
Accesses to page since server reboot:
You can use to declare methods as easily as to declare
variables
7/27/2019 Java Jsp Presentation
9/24
Softsmith Infotech 9
Directives
Directives affect the servlet class itself
A directive has the form:
or
The most useful directive is page, which lets you import
packages Example:
7/27/2019 Java Jsp Presentation
10/24
Softsmith Infotech 10
JSP Comments
Different from HTML comments.
HTML comments are visible to client.
JSP comments are used for documenting JSP code .
JSP comments are not visible client-side.
7/27/2019 Java Jsp Presentation
11/24
Softsmith Infotech 11
The include directive
The include directive inserts another file into the file being parsed
The included file is treated as just more JSP, hence it can
include static HTML, scripting elements, actions, and directives
Syntax:
The URL is treated as relative to the JSP page If the URL begins with a slash, it is treated as relative to the
home directory of the Web server
The include directive is especially useful for inserting things like
navigation bars
7/27/2019 Java Jsp Presentation
12/24
Softsmith Infotech 12
Actions
Actions are XML-syntax tags used to control the servlet engine
Inserts the indicated relative URL at execution time (not at
compile time, like the include directive does)
This is great for rapidly changing data
Jump to the (static) URL or the (dynamically computed)
JavaExpressionresulting in a URL
7/27/2019 Java Jsp Presentation
13/24
Softsmith Infotech 13
JSP in XML
JSP can be embedded in XML as well as in HTML
Due to XMLs syntax rules, the tags must be different(but they do the same things)
HTML:
XML: expression
HTML: XML: code
HTML:
XML: declarations HTML:
XML:
7/27/2019 Java Jsp Presentation
14/24
Softsmith Infotech 14
Action useBean tag
The useBean action tag is the most commonly used
tag because of its powerful features.
It allows a JSP to create an instance or receive an
instance of a Java Bean. It is used for creating or instantiating a bean with a specific
name and scope.
Examples
7/27/2019 Java Jsp Presentation
15/24
Softsmith Infotech 15
Session in jsp
In session management whenever a request comes for any
resource, a unique token is generated by the server and
transmitted to the client by the response object and stored on the
client machine as a cookie.
Session management
(i) Session Object
(ii) Cookies
(iii) Hidden Form Fields
(iv) URL Rewriting
7/27/2019 Java Jsp Presentation
16/24
Softsmith Infotech 16
Javascript
7/27/2019 Java Jsp Presentation
17/24
Softsmith Infotech 17
JavaScript language
The JavaScript language is an interpreted, object oriented basedscripting language.
JavaScript coding statements are commonly embedded inside a
webpage using the HTML tags .
When the web browser is rendering the webpage, any JavaScriptcoding statements encountered are immediately interpreted and run
before the rendering continues.
7/27/2019 Java Jsp Presentation
18/24
Softsmith Infotech 18
Where to use JavaScript
Verify the end user input data contains legitimate data values.
Handle any arithmetic or logic processing that needs to be executed
on the web browser.
Directly manipulate the web browser objects such as the status bar,
address bar, and the web browser's rendered controls (such astextboxes, selection lists, etc...).
Handle and process client-side events that have been generated.
7/27/2019 Java Jsp Presentation
19/24
Softsmith Infotech 19
How to run JavaScripts
JavaScript statements are interpreted and run by the web browser
Most common web browsers contain a JavaScript interpreter program
interpret and run the JavaScript commands that have been embeddedinside a webpage.
Question1: In a web application, which entity executes JSPs?
Question2: In a web application, which entity executes JavaScript commands?
7/27/2019 Java Jsp Presentation
20/24
Softsmith Infotech 20
JavaScript programming example
2 JavaScript Example1
3
4 Javascript Example1
5 An alert box will appear using the window object
67 8 window.alert("hello world");9 10
11 Goodbye!12
13
14
7/27/2019 Java Jsp Presentation
21/24
Softsmith Infotech 21
Events
An eventis an identifiable occurrence or action that has taken place in
the system. Writing programs that generate and handle events is
known as event programming.
Events are typically generated in the following ways.
GUI programs can generate an event to indicate there has beensome interaction between the widget and the end user.
when an end user presses a button, a click event is generated.
Input/output programs can generate an event to indicate an
input/output event has occurred.
A mouse event is generated when the end user moves themouse.
7/27/2019 Java Jsp Presentation
22/24
Softsmith Infotech 22
Binding Events
7/27/2019 Java Jsp Presentation
23/24
Softsmith Infotech 23
Binding Events
In client side web programming, the web browsercontains
it's own event listener.
Therefore you must do two things
in order to process an event that has been generated.
1. Write the code that binds the event to the event handler.
2. Write the code for the event handler.
7/27/2019 Java Jsp Presentation
24/24
Softsmith Infotech 24
Common web browser events
Event Description
click Used to indicate an entity (usually a control) has been clicked
submit Used to indicate the submit button has been pushed
mouseover Used to indicate the mouse cursor has been moved over the entity. Typically the entity is a control
or an anchor.
mouseout Used to indicate the mouse cursor has been moved away from the entity. Typically the entity is a
control or an anchor.
load Used to indicate the webpage has been completely parsed and loaded into the web browser.
unload Used to indicate the webpage has been unloaded from the web browser. Unloading a webpage
occurs when the web browser is about to load another webpage. The unload event is the last event
that will be generated from the current webpage before the next webpage is loaded. When the web
browser is closed or exited, the unload event is not generated.
Top Related