Please enable JavaScript.
Coggle requires JavaScript to display documents.
CHAPTER 3: FUNDAMENTALS OF JSP - Coggle Diagram
CHAPTER 3: FUNDAMENTALS OF JSP
Introduction
Java Servlet technology is the foundation of all the Web application technologies
Servlets are Java classes that dynamically process requests and construct
responses.
JSP pages are text-based documents that execute as servlets but allow a more natural approach to creating static content
JavaServer Pages Standard Tag Library (JSTL) encapsulates as simple tags the core functionality common to many Web applications
What is JSP file?
JSP runs on a server and respond to requests from clients
JSP executes inside a JSP container on the Web-server or Application server.
Java Server Pages (JSP) is an HTML page with some Java code added in it
HTML tags
JSP elements (Java code in HTML page)
.jsp extension
JSP Application Architecture
Based on Model-View-Controller framework
JSPs - designed to render the contents.
JSP use JavaBeans to represent business logic model
Servlet - request the processing and computations.
Known as page centric
JavaBeans – provide a business logic model
JSP page deals the request and response entirely by itself
How is JSP processed?
Web server accept the requested .jsp file & send to JSP Servlet Engine
If first time called, JSP file is parsed to JSP Servlet Engine.
Generate servlet source code (all HTML code converted into println statement)
Then, servlet code is compiled into a class file (bytecode).
Instantiate servlet.
JSP request sent to the Web Server
Sent generated servlet output (in HTML/jsp format) to web browser
User make request through web browser
Finally, HTML result are displayed on the web browser.
Structure of a Web Project
There are two kind of structures
The structure of the web application in a server
The structure of the IDE
A web project have three main elements
The Java classes
The configuration file web.xml
The JSPs files
The structure of JSP Page/JSP tags
Action Elements
JSP actions are coded using strict XML syntax form
There are many JSP action tags or elements. Each JSP action tag is used to perform some specific tasks.
The JSP action tags are given below
<jsp:forward>
: forward this HTTP request to another page or
servlet for processing
<jsp:include>
: : invoke another resource and merges its output
stream with JSP page output stream.
<jsp:param>
; binding a value to a name and passes
the binding to another resource invoked with <jsp:include> or <jsp:forward>
<jsp:fallback>
: to specify the message shown on the
browser if applets is not supported by browser.
>jsp:getProperty>
: prints the value of property of the bean
<jsp:plugin>
: to generate appropriate HTML linkage for
downloading the Java Plugin or to include applet/JavaBean
<jsp:useBean>
: creates or locates bean object
<jsp:setProperty>
: sets the vealue of property in bean object
JSP Standard Action is high level JSP elements used to create, modify, or use other objects.
Comment
JSP Comment : <%-- MyComment --%
Scripting language comment : <% /
MyComment
/ %>
Use as HTML comment : <!– MyComment -->
Scripting Elements
JSP Scriplet
The Java code placed inside the tag MUST end with semicolon ;
Sample syntax <% iNum = 10;
iNum = calculateSquare(iNum); out.print(“iNum =
” + iNum); %>
Syntax of JSP Scriptlets: <% //some lines of Java code %>
Can used JSP scriptlets to implement the flow of control on business logic.
JSP construct that allow to declare/define ANY Java code that used in JSP page
JSP Declaration
Allow to declare/define variables and methods that used in JSP page
Syntax of JSP declaration: <%! //declare variables/methods %>
The Java code placed inside the tag MUST end with semicolon ;
Sample syntax <!% String myName; %>
JSP Expression
It is mainly used to print the values of variable or method.
Syntax of JSP Expression: <%= //some Java expression %>
The expression is computed and the result is included in the HTML page that’s returned to the browser
The Java code placed inside the tag NOT end with semicolon. ;
Allow to insert Java values (values of variable or method)
Sample syntax <%= iNum %> ,<%= new java.util.Date() %>
Directive Elements
JSP process is divided into two (2) phases;
1st phase-translation time : JSP engine convert .jsp page into a servlet
2nd phase- request time : servlet is run and generate HTML tag and send response to browser
The JSP engine handles the directives at translation time -> JSP
engine will translate a specific directives into servlet (only for first time compilation)
These directives provide directions and instructions to the container, telling it how to handle certain aspects of the JSP processing.
Syntax: <%@ directive-type attribute="value" %>
JSP Directive elements are used in the JSP file and provide
nformation to the JSP container about the page.
Three type of JSP Directive
page directive
Only take place once the JSP page is compiled by JSP engine
page directives can be coded anywhere in JSP page, but the page
directives are normally coded at the top of the JSP page
page directive defines attributes that apply to an entire JSP page.
page has a lot of attributes (see below) that specify conditional
information to the JSP container for the page
Example of using : if we want to use Java class for utilities
Syntax: <%@ page attributes %>
include directive
include directive will tell the JSP container to include another file in the position it is given
Many include directives can be coded anywhere in JSP page where it is needed.
include directive allows to include source code (static resource) of another file inside JSP at specified place at translation time
Example of common include file: headers, footers, tables, navigation that are common to multiple JSP pages
Important: The include file MUST NOT BE a dynamic JSP file (ie; does not to be a complete and valid JSP or servlet).
Syntax: <%@ include attributes %>
Sample syntax: <%@ include file=“/folder_name/file_name.jsp” %>
If the include file and JSP file in the same folder, then folder name is not required in syntax.
<%@ include file=“file_name.jsp” %>
taglib directive
taglib directive specifies libraries of tag files that will be used in the current JSP file.
It can either be a Custom JSP Tag Library or JSP Standard Tag Library (JSTL)
taglib directive is custom defined JSP tags.
taglib directive is used to notify JSP engine/container that JSP page relies on one or more custom
actions.
The beauty of the tag file is that you can invoke them with attributes from within JSP pages and get some results back
Example of using : for paging, table generator, etc.
Syntax: <%@ taglib attributes %>
Sample Syntax: <%@ taglib uri=“tag_prefix” prefix=“tg” %>
uri -> the absolute path (URL) for the Tag Library Descriptor
prefix -> A unique prefix used to identify custom tags from library that being used in JSP page
Best Practice
Avoid dumping thousands of lines of code in a JSP
Refactor this using a separate Java class -> make use of MVC framework
Minimize the amount of scriptlet and declaration codes in a JSP
Calling a Java Class from JSP
Java class will have all the business logic codes
JSP gets the results from Java class and continues on with its processing
JSP file will call a separate Java class
JSP Built-in Objects:
Built-in server objects
It’s free, no need to create them
Use them directly in JSP page
List of commonly used JSP objects
response
: provides HTTP support for sending response
out
: JspWriter for including content in HTML page
request
: Contains HTTP request headers and form data
session
Unique session for each user of the Web Application
application
: shared data for all users of the web application