0% found this document useful (0 votes)
7 views

Slide 5 Java Server Pages

The document discusses JavaServer Pages (JSP) technology. It introduces JSP, describes how JSP works, and explains basic JSP tags like comments, directives, declarations, expressions and scriptlets. It also covers implicit objects and action tags in JSP.

Uploaded by

newke1689
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Slide 5 Java Server Pages

The document discusses JavaServer Pages (JSP) technology. It introduces JSP, describes how JSP works, and explains basic JSP tags like comments, directives, declarations, expressions and scriptlets. It also covers implicit objects and action tags in JSP.

Uploaded by

newke1689
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

JavaServer Pages (JSP)

Objectives
◼ JavaServer Pages (JSP) introduction
◼ How JSP Works?
◼ Basic tags
◼ Comment
◼ Directive
◼ Declaration
◼ Expression
◼ Scriplets
◼ Implicit Objects
◼ Action tags
◼ Exception handling

Contact me @ https://www.facebook.com/quynhtran.ly.94
JavaServer Pages (JSP)
◼ JavaServer Pages (JSP) technology provides a
simplified, fast way to create web pages that
display dynamically-generated content.
◼ JSP is an extension to Servlet, provides more
functionality than servlet like EL, JSTL..
◼ The JSP specification is an important part of the
Java 2 Platform, Enterprise Edition.
◼ The first place to check for information on JSP
technology is
https://www.oracle.com/java/technologies/jspt.html
◼ Presentation layer for MVC architecture: separate
the dynamic content of a Web page from its
presentation
Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP introduction
◼ Middle-tier
◼ Presentation-tier
Web Server
(Servlet Container)

HTTP
JDBC
Request
Browser
Servlet Database
(client)
Server
1st tier
(3rd tier)
Response

JSP
Request

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Page
• JSP page consists of HTML tags and JSP tags. The JSP
pages are easier to maintain than Servlet because
we can separate designing and development.
• A JSP page has the extension .jsp; this signals to the
web server that the JSP engine will process elements
on this page.
• Pages built using JSP technology are typically
implemented using a translation phase that is
performed once, the first time the page is called.
The page is compiled into a Java Servlet class and
remains in server memory, so subsequent calls to
the page have very fast response times.

Contact me @ https://www.facebook.com/quynhtran.ly.94
Simple JSP Page

Contact me @ https://www.facebook.com/quynhtran.ly.94
Example
HTML tag
<HTML>
<HEAD><TITLE>MyFirstProgram.jsp</TITLE></HEAD>
<BODY> Comment
<%-- MyFirstProgram.JSP --> JSP
Directive

<%@ page import = "java.util.Date" %> JSP


Scriptlet
<% out.println("Hello there!"); %> <Br>
JSP
Expression

<%= "Current date is " + new Date() %>


</BODY> </HTML>
Contact me @ https://www.facebook.com/quynhtran.ly.94
How is JSP processed

Contact me @ https://www.facebook.com/quynhtran.ly.94
How JSP Works?
User Request – JSP File Requested

Server

Create Servlet from JSP File


Translation Changed

Servlet Compile Execute Servlet

Contact me @ https://www.facebook.com/quynhtran.ly.94
Class Hierarchy Generated Servlet

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Lifecycle
Servlet from JSP
Init Event jspInit()

Request
Response
jspService()

Destroy Event jspDestroy()

Contact me @ https://www.facebook.com/quynhtran.ly.94
HTML Comment
• Generates a comment that is sent to the client.

◼ Syntax

<!-- comment [ <%= expression %> ] -->

◼ Example:
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %>
-->

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Directives
• Directives are used to control how the web
container translates and executes the JSP page
• Directives have attributes that dictate their meaning
and effect. In almost all cases, the effects produced
by directives can’t be replicated using expressions,
scriptlets, or declarations.
• We’ll consider the three directives:
◼ page,
◼ include,
◼ taglib.

Contact me @ https://www.facebook.com/quynhtran.ly.94
The page Directive
◼ The page directive defines attributes that apply to an
entire JSP page
<%@ page attributeName=“value" %>
◼ Page attribute

Contact me @ https://www.facebook.com/quynhtran.ly.94
The page Directive
◼ import to create import statements in the generated
servlet source produced at translation phase.
import=“java.util.*,model.*”
◼ contentType is a String that specifies the MIME type of
the response to be sent back. The default value is
"text/html;charset=ISO-8859-1"
contentType=“text/html;charset=UTF-8”
◼ pageEncoding set the character encoding of the page
source.
pageEncoding=“UTF-8”
◼ buffer sets the buffer size in kilobytesThe default size
of the buffer is 8Kb.

Contact me @ https://www.facebook.com/quynhtran.ly.94
Other attributes of the page directive

Contact me @ https://www.facebook.com/quynhtran.ly.94
The include Directive
• A JSP source file and the pages it includes through
the include directive are collectively referred to as a
“translation unit.”
• The file type you include doesn’t have to be JSP
page source, nor does it have to have a .jsp
extension. Once included, the file contents must
make sense to the JSP translation process, but this
gives scope to include entire HTML or XML
documents, or document fragments.
• Defining Implicit Includes with JSP property group
Contact me @ https://www.facebook.com/quynhtran.ly.94
The include Directive…
<%@ include file=“relativeURL " %>
<html>
<body>
<%@ include file="header.jsp" %>
<h1>Including Page</h1>
<%@ include file="footer.html" %>
</body>
</html>

•The taglib directive makes custom tags available in the JSP


page by referencing a tag library
Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP scripting elements
• Scripting elements are inserted into the JSP
page’s servlet class
• Declarations
<%! Member variable and method of servlet %>

• Expressions
<%= expression%>

• Scriplets
<% java code %>

Contact me @ https://www.facebook.com/quynhtran.ly.94
Declaration
• Declares variable or method valid in the scripting
language used in the JSP page.
◼ Syntax
<%! declaration; [ declaration; ]+ ... %>

◼ Examples
<%! String destin; %>
<%! public String getDestination()
{return destin;}%>
<%! Circle a = new Circle(2.0); %>
◼ You can declare any number of variables or methods within one
declaration element, as long as you end each declaration with a
semicolon.
◼ The declaration must be valid in the Java language.

◼ variable or method declared are members of servlet generated

Contact me @ https://www.facebook.com/quynhtran.ly.94
Declaration

Contact me @ https://www.facebook.com/quynhtran.ly.94
Expression
• Contains an expression valid in the scripting language used
in the JSP page.
◼ Syntax
<%= expression %>
Example
<%! String name = new String(“JSP World”); %>
<%! public String getName() { return name; } %>
<B><%= getName() %></B>
◼ Description:

An expression element contains a scripting language expression that is


evaluated, converted to a String, and inserted where the expression
appears in the 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 JSPfile. Expressions are evaluated from left to right.
◼ Code generated into service() method of the translated servlet

Contact me @ https://www.facebook.com/quynhtran.ly.94
Expression

Contact me @ https://www.facebook.com/quynhtran.ly.94
Scriptlet
• Contains a code fragment valid in the page scripting
language.
◼ Syntax
<% code fragment %>
<%
String var1 = request.getParameter("name");
out.println(var1);
%>
• This code will be placed in the generated servlet method:
_jspService()
• The use of scriptlet should be limited in JSP because JSP is
mainly used for presentation

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Scriptlet- Best Practice

Contact me @ https://www.facebook.com/quynhtran.ly.94
Predefined Variable – Implicit Objects
• request – Object of HttpServletRequest (request parameters, HTTP headers, cookies

• response – Object of HttpServletResponse

• out - Object of PrintWriter buffered version JspWriter

• session - Object of HttpSession associated with the request

• application - Object of ServletContext shared by all servlets in the engine

• config - Object of ServletConfig

• pageContext - Object of PageContext in JSP for a single point of access

• page – variable synonym for this object

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Session object

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Session object

Contact me @ https://www.facebook.com/quynhtran.ly.94
Predefined Variable – Implicit Objects

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP Actions
• Standard actions are used for the same purpose
as Java language-based scripting: Most if not all
the goals that you can achieve with standard
actions are achievable with other scripting
elements.
• So why use them?
◼ The answer is that they get the job done more
elegantly. They often provide an alternative to
inserting java code into your neatly designed
presentation page.

Contact me @ https://www.facebook.com/quynhtran.ly.94
Standard actions general syntax
<prefix:tagname firstAttribute="value"
secondAttribute="value">
...
</prefix:tagname>

Contact me @ https://www.facebook.com/quynhtran.ly.94
Dispatching action <jsp:include>
• The standard action <jsp:include> can be used to
include the response from another page within your
JSP page output.
• You specify the file whose response should be
included with the page attribute, like this:
<jsp:include page="pageToInclude.jsp" />
• The file whose response should be included has to
reside somewhere in your web application but
doesn’t have to be present until the page is actually
requested.

Contact me @ https://www.facebook.com/quynhtran.ly.94
<jsp:include> vs. <%@ include %>

<jsp:include>
Contact me @ https://www.facebook.com/quynhtran.ly.94
<jsp:include> vs. <%@ include %>

<%@ include %>

Contact me @ https://www.facebook.com/quynhtran.ly.94
Dispatching action <jsp:forward>
• The purpose of this standard action is to forward
processing to another resource within the web
application.

<jsp:forward page=“destinationPage">
<jsp:param name=“.." value=“.." />

</jsp:forward>

Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP exception handling
◼ Translation and compilation errors
◼ Both the translation and the compilation phases can
yield errors that are observed only when the page is
requested for the first time.
◼ If an error is encountered during either phase, the
server will return JasperException and a message
that includes the name of the JSP page and the line
where the error occurred.
◼ Runtime Exception
◼ Occurs during serving request
◼ Catch by error page for specific exception
◼ Define error pages for the whole web app in web.xml
Contact me @ https://www.facebook.com/quynhtran.ly.94
JSP exception handling
◼ The page directive in JSP provides two
attributes to be used in exception handling.
◼ errorPage: Used to site which page to be
displayed when exception occurred
◼ isErrorPage : Used to mark a page as an error
page where exceptions are displayed
◼ <%@page isErrorPage="true“ %>
◼ <%@page errorPage=“errorhandle.jsp“ %>
◼ exception implicit object available only on error
page
◼ use to send customized error message to the client.

Contact me @ https://www.facebook.com/quynhtran.ly.94
Summary
• JavaServer Pages (JSP)
◼ Presentation tier
◼ JSP Page
◼ How JSP Works?
◼ JSP Lifecycle
◼ JSP Tags
◼ JSP Implicit Objects
◼ JSP Action
◼ JSP error handling

Contact me @ https://www.facebook.com/quynhtran.ly.94
Constructive question
◼ Why should you limit the use of scriptlets in JSP?
◼ Design views for customer registration of a web
application.
◼ How will the JSP page meet the needs of multiple
concurrent users?
◼ Compare 2 variables declared by declaration tag and by
scriptlet
◼ Compare two include mechanisms using directives and
actions
◼ When you want to redirect users to login to their role-
specific page, should use sendRedirect or action forward?

Contact me @ https://www.facebook.com/quynhtran.ly.94

You might also like