ch. 7 beeing a jsp

26
Chapter 7 Being a JSP

Upload: manolis-vavalis

Post on 06-Jul-2015

284 views

Category:

Education


1 download

DESCRIPTION

Being a JSP

TRANSCRIPT

Page 1: Ch. 7 beeing a jsp

Chapter 7Being a JSP

Page 2: Ch. 7 beeing a jsp

A JSP is a servlet

Page 3: Ch. 7 beeing a jsp

How many times I’ve been accessed

Page 4: Ch. 7 beeing a jsp

Fully qualified class or Import

Page 5: Ch. 7 beeing a jsp

Questions

• Where each part of the JSP goes into the servlet code?

• May I ServletContext and ServletConfig?

• Type and syntax of the elements?

• Lifecycle?

Page 6: Ch. 7 beeing a jsp

Elements

• Scriptlet: <% %>

• Directive: <%@ %>

• Expression: <%= %>

• Declaration: <%! %>

Page 7: Ch. 7 beeing a jsp

Directives, Scriptlets & Expressions

Page 8: Ch. 7 beeing a jsp

Beware!

Expressions become arguments in out.print()

Page 9: Ch. 7 beeing a jsp

But JSPs are servlets

Page 10: Ch. 7 beeing a jsp

Declaration!

<%! int count=0; %>

Page 11: Ch. 7 beeing a jsp

Importing a variable

Page 12: Ch. 7 beeing a jsp

JSP Declatations

Instance Variable

Local Variable

Page 13: Ch. 7 beeing a jsp

Importing methods & variables

Page 14: Ch. 7 beeing a jsp

JSP lifecycle• Look at the directives

• Create an HttpServlet subclass

• Writes • Import statements • Declaration statements

• Builds a service method _jspService()

• Combines all

Page 15: Ch. 7 beeing a jsp

Me Container, you JSP

I will • give you the class

• I might not show the generated Java • look at your directives • create an HttpServlet subclass

• Tomcat extends: org.apache.jasper.runtime.HttpJspBase • write any imports or declarations statements in the class file,

• imports just below the package statement • declarations below the class declaration and before the service method

• build the service method _jspService() • called by the servlet superclass’ overridden service() method, and • receives HttpServletRequest & HttpServletResponse.

• declare & initialize all implicit objects • combine HTML, scriptlets & expressions into the service method

You should • not worry on how I do the above • Know how your elements work inside the generated servlet

• and what jspInit(), jspDestroy & _jspService() are about

Page 16: Ch. 7 beeing a jsp

Example

Page 17: Ch. 7 beeing a jsp

API and implicit objects

Page 18: Ch. 7 beeing a jsp

API and the generated servlet

Page 19: Ch. 7 beeing a jsp

Lifecycle of a JSP

Page 20: Ch. 7 beeing a jsp

Initializing your servlet JSP

Overwrite using declaration

Page 21: Ch. 7 beeing a jsp

Attributes in a JSP

Page 22: Ch. 7 beeing a jsp

Using PageContext

Page 23: Ch. 7 beeing a jsp

Other (page) directives & attributes

<%@ page import=”foo.*” session=”false” %> <%@ taglib tagdir=”/WEB-INF/tags/cool” prefix=”cool” %> <%@ include file=”wickedHeader.html” %>

Page 24: Ch. 7 beeing a jsp

EL (Expression Language)

• Web page designers shouldn’t have to know Java.

• Java code in a JSP is hard to change and maintain.

Page 25: Ch. 7 beeing a jsp

We have a choice

Page 26: Ch. 7 beeing a jsp

One last word: Actions (details later)