Lecture 35
Lecture 35
Lecture 35
JavaServer Pages
We have started JSP journey in the last handout and thoroughly discussed the JSP
scripting elements. JSP directive elements and implicit objects will be discussed in this
handout. Let’s review JSP journey again to find out what part we have already covered.
Directive Elements
– Provides global control of JSP ……..…………….. <%@ %>
Scripting Elements
– JSP comments ……………………………………... <%-- --%>
We start our discussion from implicit objects. Let’s find out what these are?
Implicit Objects
To simplify code in JSP expressions and scriptlets, you are supplied with eight
automatically defined variables, sometimes called implicit objects. The three most
important variables are request, response & out. Details of these are given
below:
– request
This variable is of type HttpServletRequest, associated with the request. It
gives you access to the request parameters, the request type (e.g. GET or POST),
and the incoming HTTP request headers (e.g. cookies etc).
– response
This variable is of type HttpServletResponse, associated with the response
to client. By using it, you can set HTTP status codes, content type and response
headers etc.
- 434 -
Handout 35
Web Design & Development CS-506
– out
This is the object of JspWriter used to send output to the client.
web.jsp
if page == web
index.jsp controller.
jsp
if page == java
java.jsp
index.jsp
<html>
<body>
<h2>Select the page you want to visit</h2>
<h3>
<input type="radio" name = "page" value="java"/>
Java
</h3>
- 435 -
Handout 35
Web Design & Development CS-506
<br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
controller.jsp
<html>
<body>
</body>
</html>
web.jsp
<html>
<body>
- 436 -
Handout 35
Web Design & Development CS-506
java.jsp
<html>
<body>
------------------------
- 437 -
Handout 35
Web Design & Development CS-506
– session
This variable is of type HttpSession, used to work with session object.
– application
This variable is of type ServletContext. Allows to store values in key-value
pair form that are shared by all servlets in same web application/
– config
This variable is of type ServletConfig. Represents the JSP configuration
options e.g. init-parameters etc.
– pageContext
This variable is of type javax.servlet.jsp.PageContext, to give a
single point of access to many of the page attributes. This object is used to stores
the object values associated with this object.
– exception
This variable is of type java.lang.Throwable. Represents the exception
that is passed to JSP error page.
– page
This variable is of type java.lang.Object. It is synonym for this.
-438 -
Handout 35
Web Design & Development CS-506
JSP Directives
JSP directives are used to convey special processing information about the page to JSP
container. It affects the overall structure of the servlet that results from the JSP page. It
enables programmer to:
Format
<%@ directive {attribute=”val”}* %>
In JSP, there are three types of directives: page, include & taglib. The formats
of using these are:
- 439 -
Handout 35
Web Design & Development CS-506
Format
<%@include file=“relativeURL”%>
Purpose
To include a file in a JSP document at the time document is translated into a servlet. It
may contain JSP code that affects the main page such as response page header settings
etc.
- 440 -
Handout 35
Web Design & Development CS-506
This example contains three JSP pages. These are index.jsp, header.jsp &
footer.jsp. The header.jsp will display the text of “web design and
development” along with current date. The footer.jsp will display only “virtual
university”. The outputs of both these pages will be included in index.jsp by using
JSP include directive.
header.jsp
<%@page import="java.util.*"%>
<html>
<body>
<marquee>
<h3> Web Desing & Development </h3>
<h3><%=new Date()%></h3>
</marquee>
</body>
</html>
footer.jsp
<html>
<body>
<marquee>
<h3> Virtual University </h3>
</marquee>
</body>
</html>
index.jsp
<html>
<body>
- 441 -
Handout 35
Web Design & Development CS-506
<TABLE BORDER=1>
<TR><TH></TH><TH>Apples<TH>Oranges
<TR><TH>First Quarter<TD>2307<TD>4706
<TR><TH>Second Quarter<TD>2982<TD>5104
<TR><TH>Third Quarter<TD>3011<TD>5220
<TR><TH>Fourth Quarter<TD>3055<TD>5287
</TABLE>
</body>
</html>
index.jsp
<html>
<body>
<TABLE BORDER=1>
<TR><TH></TH><TH>Apples<TH>Oranges
<TR><TH>First Quarter<TD>2307<TD>4706
<TR><TH>Second Quarter<TD>2982<TD>5104
<TR><TH>Third Quarter<TD>3011<TD>5220
<TR><TH>Fourth Quarter<TD>3055<TD>5287
</TABLE>
</body>
</html>
-442 -
Handout 35
Web Design & Development CS-506
jspInit()
Request _jspService()
Response
jspDestroy()
- 443 -
Handout 35
Web Design & Development CS-506
References:
- 444 -