3 - JSP (Java Server Pages)
3 - JSP (Java Server Pages)
▪ Introduction to JSP
▪ JSP scripting element
▪ JSP Implicit objects
▪ JSP Directives
OUTLINE ▪ MVC architecture
▪ Create and Run simple JSP & Servlet application
▪ Introduction to EJB
▪ Types of EJBs and usage
▪ P2P and Publisher Subscriber messaging
INTRODUCTION TO JAVA SERVER PAGES (JSP)
It is more convenient to write regular HTML than to have plenty of println statements that
generate the HTML.
Example:
<%!
Public void jspDestroy() {
System.out.println(“JSP Destroyed”);
}
Public void jspInit() {
System.out.println(“JSP Loaded”);
}
int myVar = 123;
%>
The functions and variables defined are available to the JSP Page as well as to the servlet
in which it is compiled
SCRIPTLETS (<% AND %>)
Used to embed java code in JSP pages.
▪ Contents of JSP go into _JSPpageservice() method
▪ Code should comply with syntactical and semantic construct of java
▪ Embedded in <% and %> delimiters
Example:
<%
int x = 5;
int y = 7;
int z = x + y;
%>
EXPRESSIONS (<%= AND %>)
Used to write dynamic content back to the browser.
▪ If the output of expression is Java primitive the value is printed back to the browser
▪ If the output is an object, then the result of calling toString on the object is output to
the browser
▪ Embedded in <%= and %> delimiters
Example:
2. Extends: Declares the class which the servlet compiled from JSP needs to extend
3. Import: Declares the packages and classes that need to be imported for using in the java code (comma separated list)
4. Session: (Default true) Boolean which says if the session implicit variable is allowed or not. Sometimes we don't need a
session to be created in JSP, and hence, we can set this attribute to false in that case.
5. Buffer: defines buffer size of the jsp in kilobytes (if set to none no buffering is done)
6. autoFlush: When true the buffer is flushed when max buffer size is reached (if set to false an exception is thrown when
buffer exceeds the limit)
7. isThreadSafe: (default true) If false the compiled servlet implements SingleThreadModel interface
8. Info: String returned by the getServletInfo() of the compiled servlet
9. errorPage: Defines the relative URI of web resource to which the response should be forwarded in case of an exception
10. contentType: (Default text/html) Defines MIME type for the output response
11. isErrorPage: True for JSP pages that are defined as error pages
12. pageEncoding: Defines the character encoding for the jsp page
PAGE DIRECTIVES EXAMPLES
response This is the HttpServletResponse object associated with the response to the client.
out This is the PrintWriter object used to send output to the client.
session This is the HttpSession object associated with the request.
pageContext This encapsulates use of server-specific features like higher performance JspWriters.
This is simply a synonym for this, and is used to call the methods defined by the translated servlet
page
class.
Exception The Exception object allows the exception data to be accessed by designated JSP.
IMPLICIT OBJECTS– JSP
REDIRECTION DEMO
JSP → JSP redirection using response implicit object
Processed during the request processing phase.
As opposed to JSP directives which are processed
during translation
Standard actions should be supported by Java EE
compliant web servers
JSP ACTIONS Custom actions can be created using tag libraries
The different actions are
JSP ACTIONS 2
jsp:useBean
Finds or instantiates a JavaBean.
jsp:setProperty
3 Sets the property of a JavaBean.
jsp:getProperty
4 Inserts the property of a JavaBean into the output.
jsp:forward
5 Forwards the requester to a new page.
jsp:plugin
6 Generates browser-specific code that makes an OBJECT or EMBED tag for the
Java plugin.
jsp:element
7 Defines XML elements dynamically.
jsp:attribute
8 Defines dynamically-defined XML element's attribute.
jsp:body
9 Defines dynamically-defined XML element's body.
jsp:text
10 Used to write template text in JSP pages and documents.
MODEL-VIEW-CONTROLLER (MVC)
View
Views are the presentation layer of MVC framework. Views
MVC are basically the templates where we write the scripts.
Mainly these scripts consist HTML, JavaScript, etc.
Controller
Controllers are basically the classes which are responsible
to handle the user's request. Controllers deal with the flow
of the MVC application, they handle the data coming from
the user and responds with a relevant View with a specific
Model if required.
ENTERPRISE JAVABEANS (EJB)
A framework, or software
framework, is a platform for
developing software applications.
WHAT IS A FRAMEWORK It provides a foundation on which
software developers can build
programs for a specific platform.
ADVANTAGES OF USING A FRAMEWORK
Most of the popular frameworks are open-source.
In most cases, the framework has good documentation and support.
Frameworks eliminate the need to write a lot of repetitive code that you will
find being used in many different applications.
The advantage of efficiency will never be underestimated.
As far as a framework usually developed and tested by many different
developers it can gain a strong level of security.
Integration: for building almost any type of application where you want to
store some data, you will typically use a database. There also exist many
other tools that link to web development. Many frameworks will thus make it
easier to link to these tools and also communicate with them.
INTRODUCTION TO EJB
Enterprise JavaBeans (EJB) is a specification/framework
for developing large-scale, distributed business
applications on the Java platform.
To run EJB application, you need an application server
(EJB Container) such as Wildfly, Glassfish, Weblogic…etc.
It performs,
▪ life cycle management
▪ security
▪ transaction management
▪ object pooling
WHEN USE TO ENTERPRISE JAVA BEAN?
ADVANTAGES OF EJB
not require you to write:
• Multi-threaded, multiple access code
• Database access code (e.g. JDBC)
• Network communication code (i.e. it uses RMI) for
client/server communication
• Network communication code for EJB to EJB communication
• Transaction management code
Entity Bean
Entity beans represent persistent data storage. User data can be saved to database via
entity beans and later on can be retrieved from the database in the entity bean.
Each message is delivered to one and only one Each message is delivered to multiple
JMS Receiver Consumers.
P2P Model has no timing dependency. Pub/Sub model has some timing dependency.
Welcome.jsp
Welcome, <username> !!
QUESTIONS
1. Explain the MVC architecture
2. Explain how the servlet container handles a dynamic request.
3. Explain the life cycle of a Servlet by using life cycle methods of a servlet.
4. What are the differences between JSP “include directive” and “include action”?
5. Describe what is a framework and list down the advantages of using a framework.
6. What are the main differences between stateless session beans and stateful session beans?
7. Describe Distributed application architecture with the aid of a diagram.
8. Briefly explain what multi-tiered architecture is.
9. List down the types of Enterprise Java beans and their usage.
10. Explain what is Java Message Service (JMS) and how it can be implemented in EJB.
11. Briefly explain Point-to-Point and Publisher/Subscriber messaging domains.
THANK YOU Q&A