Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Module 2
Agenda
1 Introduction JSP
2 JSP Architecture
3 Elements of JSP
4 JavaBean
Objectives
JSP lies in the presentation tier on the web server, with the
main responsibility of generating HTML content that needs
to be served to the browser. It also has the additional
responsibility of pass on the requests to the backend
through the JavaBeans, as and when required.
Notes
• The above picture brings a perspective on where JSP fits in the 3-tier
web application architecture.
• JSP lies in the presentation tier on the web server, with the main
responsibility of generating HTML content that needs to be served to
the browser. It also has the additional responsibility of pass on the
requests to the backend through the JavaBeans, as and when
required.
JSP: What makes JSP so attractive?
1. High performance
4. Powered by Java
– Has access to all Java APIs
– Has access to all J2EE APIsa
– Inherent Platform independence
Notes
• allow the user to create their own utility, or business logic classes
• Typically HTTP requests are sent to the web server from a browser
client. If the request if for a static HTML page, the web server itself
responds. However, if the request is for a JSP or a servlet, the add-
ons to a web server viz. servlet engine or JSP engine respond. These
engines require a JVM as, they are themselves Java-based.
The Architecture: The flow of JSP request
Notes
• The flow of a JSP request:
1.The HTTP request comes to a web server.
2.The server extension receives the request (as it is not a request for
static HTML but for a JSP) and passes it on to the JSP engine.
3.The JSP engine invokes the JSP parser to parse the JSP and check for
any syntax errors.
4.On successful parsing, the JSP engine invokes the Java compiler to
compile the JSP into an equivalent servlet class.
5.Once the class is generated, the control is passed on to a servlet
engine.
6.The servlet engine loads the servlet into memory using its class loader.
7.The appropriate methods in the servlet are invoked and the response is
routed back to the browser via the server extension and web server.
The Architecture: JSP Architecture Models
Application Server
Client
1. Request
Browser JSP
4. Response
2. Creates
JavaBeans
3. Retrieves Data
JavaBeans
DB
Notes
Lets you insert Java code into the servlet that will be
generated from the current JSP page.
There are three forms:
• Expressions
Useful shorthand for printing out strings and
contents of variables.
• Scriptlets
Lets you insert any valid Java code into the JSP.
• Declarations
Useful for declaring page wide variables and
methods(Java) or functions.
Notes
• Expressions
• Useful shorthand for printing out strings and
contents of variables.
• Scriptlets
• Lets you insert any valid Java code into the
JSP.
• Declarations
• Useful for declaring page wide variables and
methods (Java) or functions.
Scripting Elements: Expressions
A JSP expression is used to insert Java values directly into the output.
<%= Java Expression %>
• For example, the following shows the date/time that the page was
requested: Current time: <%= new java.util.Date() %>
<html>
<head><title>Request Header Info</title></head>
<body bgcolor="white">
<ul>
<li>Request Method: <%=request.getMethod() %>
<li>Request URL :<%=request.getRequestURI()%>
<li>Request Protocol :<%=request.getProtocol() %>
<li>Server Name :<%=request.getServerName()%>
<li>Server Port :<%=request.getServerPort()%>
<li>Remote Address: <%=request.getRemoreAddr() %>
<li>Browser :<%=request.getHeader("User-Agent") %>
</ul>
</body>
</html>
The out object Example
<html>
<head><title>The Out Object Example</title></head>
<body bgcolor="white">
<% out.print("<h3> JSP BufferInformation</h3>");
<%-- display the page buffer size --%>
Buffer:<%=out.getBufferSize() %> bytes <br/>
<%-- display the AutoFlush setting -- %>
AutoFlush: <%=out.isAutoFlush() %> <br/>
<%-- display the free page buffer space --%>
Remaining buffer : <%=out.getRemaining() %> bytes
</body>
</html>
The Response Object Example
<html>
<head><title>Response Object Info</title></head>
<body bgcolor="white">
<% response.setContentType("text/html"); %>
Buffer Size: <response.getBufferSize() %> bytes <br/>
Character Encoding :<%=response.getCharacterEncoding()
%>
<br/>
Locale: <%=response.getLocale() %>
<%-- redirect the user to another.html --%>
<%
response.sendRedirect("http://localhost:8080/another.
html"); %>
</body>
</html>
The session object Example
<html>
<head><title>The Out Object
Example</title></head>
<body bgcolor="white">
Session: <%=session.getId() %><hr/>
<% session.invalidate();%>
Session : <% if(session.getId() !=null)
out.println(session.getId());
else
out.println("Session ended ") ; %>
</body>
</html>
Notes
• If you want to use the characters "%>" inside a scriptlet, enter "%\>"
instead. XML syntax scriptlet is
• <jsp:scriptlet>
• Code
• </jsp:scriptlet>
• NOTE: Declarations are executed at initialization time; Expressions
and Scriptlets at request Time.
Scriptlets Example
<html>
<head><title>Scriptlet Example</title></head>
<body bgcolor="white">
<html>
<head><title>Declaration sricpting element
Example</title></head>
<body bgcolor="white">
<body bgcolor="white">
</body>
<html>
Notes
• Additional Example on include
• <body bgcolor="blue">
• <font color="red">
• <p>Static Include :
• <%@ include file="ex2.jsp" %>
• <p>Dynamic Include :
• </font>
• <jsp:include page="ex3.jsp" flush="true"/>
• </body>
Actions: The jsp:forward Action
• Syntax
<jsp:forward page= “relativeURL" />
Notes
• Additional Example on include
• How did we arrive at the conclusion that name and empID are the
properties of this bean?
• For identifying the property of a bean, you have to group the pair of
mutator(setter) and accessor(getter) methods together.
• For ex : How many pair of setter and getter methods do we have? The
answer is two.
• 1. setName() and getName()
• 2. setEmpID() and getEmpID()
• Take the first pair. Strip off set and get. What remains is Name.
Change the case of first character(remember only first character) to
lower case. What you get is the first property. i.e. name
• When you apply the same rule to the second pair, you get the second
property. i.e. empID
Actions: The jsp:useBean Action
package p1;
public class Example1 {
private int age;
public int getAge(){
}
public void setAge(int i){
}
} If we have to instantiate the given class in java, we will
use the following syntax :
p1.Example1 obj1 = new p1.Example1();
In jsp, the same result is achieved using jsp:useBean :
<jsp:useBean id=“obj1” class =“p1.Example1” />
<jsp:getProperty>
• Converts property names following the bean standards
property: This required attribute indicates the property you want to set.
However, there is one special case: a value of "*" means that all request
parameters whose names match bean property names will be passed to the
appropriate setter methods.
value: This optional attribute specifies the value for the property. String values
are automatically converted to numbers, boolean to Boolean, byte to Byte,
char to Character via the standard valueOf method in the target or wrapper
class. For example, a value of "true" for a boolean or Boolean property will be
converted via Boolean.valueOf, and a value of "42" for an int or Integer property
will be converted via Integer.valueOf.
Notes
4. Marty Hall (1999). Java Server Pages (JSP) 1.0. Retrieved April 24, 2012, from,
http://users.polytech.unice.fr/~buffa/cours/internet/POLYS/servlets/Servlet-
Tutorial-JSP.html
5. Apl.jhu.edu (1999). Java Server Pages(JSP) 1.0.Retrieved April 24, 2012, from,
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html
6. Paul Wheaton (2012). Java - JSP. Retrieved April 24, 2012, from,
http://www.coderanch.com/t/281980/JSP/java/Hyperlinks
Thank You