Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Slide 5-Java Server Pages

Uploaded by

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

Slide 5-Java Server Pages

Uploaded by

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

JavaServer Pages (JSP)

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

06/24/24 2/26
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

06/24/24 3/26
JSP introduction
 Middle-tier
 Presentation-tier
Web Server
(Servlet Container)

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

JSP
Request

06/24/24 4
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.

06/24/24 5/26
Example
HTML tag
<HTML>
<HEAD><TITLE>MyFirstProgram.jsp</TITLE></HEAD>
Comment
<BODY> JSP
Directive
<%-- MyFirstProgram.JSP -->
JSP
<%@ page import = "java.util.Date" %> Scriptlet

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

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


</BODY>
06/24/24
</HTML> 6
How JSP Works?
User
UserRequest
Request––JSP
JSPFile
FileRequested
Requested

Server
Server

Create
CreateServlet
Servletfrom
fromJSP
JSP File
File
Translation
Translation Changed
Changed

Servlet
ServletCompile
Compile Execute
ExecuteServlet
Servlet

06/24/24 7/26
Class Hierarchy Generated Servlet

06/24/24 8/26
JSP Lifecycle
Servlet from JSP
Init Event jspInit()
jspInit()

Request
Response
jspService()
jspService()

Destroy Event jspDestroy()


jspDestroy()

06/24/24 9/26
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() %>
-->

06/24/24 10/26
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.

06/24/24 11/26
The page Directive
 The page directive defines attributes that apply to an
entire JSP page
<%@ page attributeName=“value" %>
 Page attribute

06/24/24 12/26
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.

06/24/24 13/26
Other attributes of the page directive

06/24/24 14/26
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
06/24/24 15/26
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
06/24/24 16/26
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 %>

06/24/24 17/26
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

06/24/24 18/26
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

06/24/24 19/26
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

06/24/24 20/26
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

06/24/24 21/26
Predefined Variable – Implicit Objects

06/24/24 22/26
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.

06/24/24 23/20
Standard actions general syntax
<prefix:tagname firstAttribute="value"
secondAttribute="value">
...
</prefix:tagname>

06/24/24 24/20
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.

06/24/24 25/20
<jsp:include> vs. <%@ include %>

<jsp:include>
06/24/24 26/20
<jsp:include> vs. <%@ include %>

<%@ include %>

06/24/24 27/20
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>

06/24/24 28/20
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
06/24/24 29
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.

06/24/24 30
Summary
• JavaServer Pages (JSP)
 Presentation tier
 JSP Page
 How JSP Works?
 JSP Lifecycle
 JSP Tags
 JSP Implicit Objects
 JSP Action
 JSP error handling

06/24/24 31/26
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?

06/24/24 32/20

You might also like