IT GROUP
Java Server Page
Olivier de Pertat
Why JSP
Aim : insert extra tags into the HTML pages
that performs some Java execution.
Same as PHP et ASP.
Features:
Multi
-
Platforms
Component reuse
Java Programming language
Standardized
HTML code is much more easier to maintain than
in Servlet (Goodbye
println
!)
JSP How to
The engine is a Servlet (JspServlet)
On the fly generation & compilation of class that
extends HttpJspBaseon the first invocation
Then execution of the _jspService method from a
generated object (by JspServlet)
JSP Engines
Servlets Engines
JSP might be handled by Servlets (WebSphere
3.5)
Example
<HTML>
<BODY>
<H1> Hello
<%
String pname; // déclaration de variable
pname = request.getParameter("name");
if (pname == null) {
out.println("World !");
} else { %>
Mister <%=pname%>
<% } // fin du else %>
</H1></BODY></HTML>
Implicit Objects
Variable
Type
request
HttpServletRequest
response
HttpServletResponse
out
JSPWriter (jsp)
session
HttpSession
pagecontext
PageContext (jsp)
application
ServletContext
config
ServletConfig
page
Object
Comment tags
Server comments within the HTML page
Example
<html>
<%
--
The client will never see this
!!!
--
%>
</html>
Directive Elements
Allows to give information about the page:
<%@ page keyword=«
value"%>
.
extends
: JSP page inheritance
import
: java packages we’ll use rigth there
<%@ page import="java.util.*"%>
.
errorPage
: error cutom page.
<%@ page errorPage="/error/err.jsp"%>
.
include
: file inclusion.
<%@ include file=“header.jsp"%>
.
Declaration tags
JSP Page Variable declaration (page scope)
Example
<%!
private int counter = 0;
private String getName(int i)
%>
Expression Tags
Prints out a variable value
Example
<%= new java.util.Date() %>
Script tags
Java code in the HTML page
Example
<%
String toto = "Toto";
out.println(toto);
%>
Java Bean use
A Java Bean should:
Have an empty constructor
Setters/Getters for each variable
Being Serializable
JavaBeans scope :
page : in the hole page
request : last as long long as the Request object (even on
multiple pages)
session : lasts the while session
application: last as long as the server is active
Example :
<jsp:use
id=“myBeanObj"
scope="application"
class="MyClass"
%>
Form management
<%@ method = "doPost" %>
<HTML><BODY>
<H1> Hello
<%
String pname; // déclaration de variable
pname = request.getParameter("name"); // request
: objet implicite
if (pname== null) { out.println("World"); } else
{
%>
Mister <%=pname%>
<% } // fin du else %>
</H1>
</BODY></HTML>
Cart sample
<html>
<jsp:useBean id="cart" scope="session"
class="mycybermarket.MyCart" />
<jsp:setProperty name="cart"
property="*" />
<% cart.processRequest(request);
String[] items =
cart.getItems()
;
for (int i=0; i<items.length; i++) { %>
<li> <%= items[i] %>
<% } %>
<%@ include file ="catalog.html" %>
</html>
No JSP caching
<%
response.setHeader("Cache
-
Control","no
-
cache");
response.setHeader("Pragma","no
-
cache");
response.setDateHeader ("Expires", 0);
%>
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Comments 0
Log in to post a comment