CSI 3140 WWW Structures Techniques and Standards Separating
- Slides: 85
CSI 3140 WWW Structures, Techniques and Standards Separating Programming and Presentation: JSP Technology
Why JSP? w. Servlet/CGI approach: server-side code is a program with HTML embedded w. Java. Server Pages (and PHP/ASP/Cold. Fusion) approach: server-side “code” is a document with program embedded n n Supports cleaner separation of program logic from presentation Facilitates division of labor between developers and designers Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Default namespace is XHTML Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Also uses two JSP-defined namespaces Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example JSP-defined markup (initialization) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Standard XHTML Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example JSP scriptlet Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example JSP-based program logic: initialize and increment variable Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Replaced with value of variable Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Output XHTML document after 3 visits Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example w. Used html as root element n n Can use HTML-generating tools, such as Mozilla Composer, to create the HTML portions of the document JSP can generate other XML document types as well Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example w. Namespaces n n JSP (basic elements, normal prefix jsp) Core JSP Standard Tag Library (JSTL) (prefix c) Tag library: means of adding functionality beyond basic JSP l JSTL included in with JWSDP 1. 3 version of Tomcat l JSTL provides tag libraries in addition to core (more later) l Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example w. JSP elements n n directive. page: typical use to set HTTP response header field, as shown (default is text/xml) output: similar to XSLT output element (controls XML and document type declarations) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example w. Template data: Like XSLT, this is the HTML and character data portion of the document w. Scriptlet: Java code embedded in document n n While often used in older (non-XML) JSP pages, we will avoid scriptlet use One use (shown here) is to add comments that will not be output to the generated page Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Example w. Core tag library supports simple programming n if: conditional l n empty: true if variable is non-existent or undefined set: assignment l application scope means that the variable is accessible by other JSP documents, other users (sessions) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP and Servlets w JSP documents are not executed directly n When a JSP document is first visited, Tomcat 1. 2. n Translates the JSP document to a servlet Compiles the servlet The servlet is executed w Exceptions provide traceback information for the servlet, not the JSP n The servlets are stored under Tomcat work directory Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP and Servlets w. A JSP-generated servlet has a _jsp. Service() method rather than do. Get() or do. Post() n This method begins by automatically creating a number of implicit object variables that can be accessed by scriptlets Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP and Servlets w. Translating template data: w. Scriptlets are copied as-is to servlet: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP and Servlets w. Scriptlets can be written to use the implicit Java objects: w. We will avoid this because: n n It defeats the separation purpose of JSP We can incorporate Java more cleanly using Java. Beans technology and tag libraries Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP and Servlets w. JSP elements translate to: JSP default w${visits} in template code translates to out. write() of value of variable w. Core tags (e. g. , if) normally translate to a method call Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. A web application is a collection of resources that are used together to implement some webbased functionality w. Resources include n n Components: servlets (including JSP-generated) Other resources: HTML documents, style sheets, Java. Script, images, non-servlet Java classes, etc. Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Sharing data between components of a web application n Tomcat creates one Servlet. Context object per web application Call to get. Servlet. Context() method of a servlet returns the associated Servlet. Context supports set. Attribute()/get. Attribute() methods Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Within Tomcat, all of the files of a simple web app are placed in a directory under webapps n n JSP documents can go in the directory itself “Hidden” files--such as servlet class files--go under a WEB-INF subdirectory (more later) w. Once the web app files are all installed, use Tomcat Manager to deploy the app Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w Deploying a web app consisting of a single JSP document Hello. Counter. jspx: n n Create directory webapps/Hello. Counter Copy JSP doc to this directory Visit localhost: 8080/manager/html Enter Hello. Counter in “WAR or Directory URL” box and click Deploy button w Web app is now at URL localhost: 8080/Hello. Counter/ Hello. Counter. jspx Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Manager app: n n Stop: web app becomes unavailable (404 returned) Start: web app becomes available again Reload: stop web app, restart with latest versions of files (no need to restart server) Undeploy: stop app and remove all files! l Always keep a copy of app outside webapps Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Set parameters of a web application by n n Creating a deployment descriptor (XML file) Saving the descriptor as WEB-INF/web. xml w. Simple example web. xml: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Some examples: n n Setting an initial value accessible by application. get. Init. Parameter(): Setting the length of time (in minutes) before a session times out: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Mapping URLs to app components: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. There are four URL patterns (from high to low precedence) w. If no URL pattern matches, Tomcat treats path as a relative file name Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Web Applications w. Methods on request object for obtaining path information: n n Example: /Hello. Counter/visitor/test. jsp get. Context. Path(): returns /Hello. Counter get. Servlet. Path(): returns /visitor get. Path. Info(): returns /test. jsp Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w${visits+1} is an example of an EL expression embedded in a JSP document n n ${…} is the syntax used in JSP documents to mark the contained string as an EL expression An EL expression can occur l In template data: evaluates to Java String l As (part of) the value of certain JSP attributes: evaluates to data type that depends on context Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w. EL literals: n n true, false decimal integer, floating point, scientificnotation numeric literals strings (single- or double-quoted) null Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w. EL variable names: like Java n n n Can contain letters, digits, _ , and $ Must not begin with a digit Must not be reserved: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w EL operators: n Relational: <, >, <=, >=, ==, != l n Logical: &&, ||, ! l n n n Or equivalents: and, or, not Arithmetic: l n Or equivalents: lt, gt, le, ge, eq, ne +, - (binary and unary), * /, % (or div, mod) empty: true if arg is null or empty string/array/Map/Collection Conditional: ? : Array access: [ ] (or object notation) Parentheses for grouping Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w. EL automatic type conversion n n Conversion for + is like other binary arithmetic operators (+ does not string represent concatenation) Otherwise similar to Java. Script Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w. EL provides a number of implicit objects w. Most of these objects are related to but not the same as the JSP implicit objects n JSP implicit objects cannot be accessed directly by name in an EL expression, but can be accessed indirectly as properties of one of the EL implicit objects Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w page. Context: provides access to JSP implicit objects n Ex: EL expression page. Context. request is reference to the JSP request object w page: JSP implicit object representing the servlet itself w JSP objects page, request, session, and application all have get. Attribute() and set. Attribute() methods n These objects store EL scoped variables (e. g. , visits) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w Reference to non-implicit variable is resolved by looking for an EL scoped variable in the order: n n page request session application w If not found, value is null w If found, value is Object n JSP automatically casts this value as needed Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w. All EL implicit objects except page. Context implement Java Map interface w. In EL, can access Map using array or object notation: n n Servlet: request. get. Parameter(“p 1”) EL: param[‘p 1’] or param. p 1 Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Expression Language (EL) w. Array/List access: If EL scoped variable a. Var represents Java array; or l java. util. List l and if EL scoped variable index can be cast to integer then can access elements of a. Var by a. Var[index] l a. Var. index l Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup w. Three types of markup elements: n Scripting Ex: scriptlet l Inserts Java code into servlet l n Directive Ex: directive. page l Instructs JSP translator l n Action Standard: provided by JSP itself l Custom: provided by a tag library such as JSTL l Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup w. Two JSPX directives n directive. page; some attributes: content. Type l session: false to turn off use of session object l error. Page: component that will generate response if an exception is thrown l is. Error. Page: true to access EL implicit exception object l n directive. include: import well-formed XML Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup w. Common variables: n var Name of a scoped variable that is assigned to by the action l Must be a string literal, not an EL expression l n scope Specifies scope of scoped variable as one of the literals page, request, session, or application l page default scope, unless otherwise specified l Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wset action n Setting (and creating) a scoped variable n Setting/creating an element of Map Key l Actually, this fails at run time in JWSDP 1. 3 (which treats EL implicit object Maps as read-only) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wremove action n n Only attributes are var and scope Removes reference to the specified scoped variable from the scope object <c: remove var=“visits” scope=“application” /> Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wout action n Normally used to write a string to the out JSP implicit object Automatically escapes all five XML special characters If value is null output is empty string l Override by declaring default attribute Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wurl action n value attribute value is a URL to be written to the out JSP implicit object URL’s beginning with / are assumed relative to context path param elements can be used to define parameters that will be URL encoded Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup w. Alternative to the value attribute (set and param elements) n n If element has content, this is processed to produce a String used for value Even out element will produce string, not write to the out object Assigns value of variable messy (XML escaped) to scoped variable clean Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wif action n General form includes scoped variable to receive test value Assigned Boolean value of test attribute l The element can be empty if var is present Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup w choose action <c: choose> <c: when test=“${visits eq 1}”> Hi!</c: when> <c: when test=“${visits eq 2}”> Welcome back!</c: when> <c: otherwise> You’re a regular!</c: otherwise> </c: choose> Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wfor. Each action n Used to increment a variable: n Used to iterate over a data structure: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
JSP Markup wfor. Each action n n Can iterate over array, Map, Collection, Iterator, Enumeration Elements of Map are Map. Entry, which support key and value EL properties: Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. JSTL Core actions are designed to be used for simple, presentation-oriented programming tasks w. More sophisticated programming tasks should still be performed with a language such as Java w. Java. Beans technology allows a JSP document to call Java methods Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w Example w Requirements: Java. Beans class must n n Be public and not abstract Contain at least one simple property design pattern method (defined later) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. Using a Java. Beans class in JSP Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. Using a Java. Beans class as shown: n Class must have a default (no-argument) constructor to be instantiated by use. Bean l n Automatically supplied by Java in this example Class should belong to a package (avoids need for an import) l This class would go in WEB-INF/classes/my/ directory w. Instance of a Java. Beans class is a bean Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. Simple property design patterns n Two types: getter and setter Both require that the method be public l getter: l w no arguments w returns a value w name begins with get (or is, if return type is boolean) followed by upper case letter l setter: w one argument (same type as setter return value) w void w name begins with set followed by upper case letter Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. EL calls simple property design method in response to access of bean property: n n Attempt to read property generates call to associated get/is method (or error if none exists) Attempt to assign value to property generates call to associated set method (or error) Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. Example setter method w. Calling setter from JSP Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java. Beans Classes w. Simple property design pattern methods associate bean properties with beans n Name of bean property obtained by removing get/is/set method prefix and following the rule: If remaining name begins with two or more upper case letters, bean property name is remaining name: set. AValue() AValue l If remaining name begins with a single upper case letter, bean property name is remaining name with this letter converted to lower case: get. Welcome() welcome l Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Instantiating Beans w. Beans can be instantiated by a servlet and made available to JSP via scope objects n Servlet n JSP: no need for use. Bean action ${session. Scope. test. Bean. welcome} Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Instantiating Beans wuse. Bean only instantiates a bean if one does not already exist, can optionally perform initialization Evaluated only if use. Bean instantiates Test. Bean Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Using Beans w. Example: mortgage calculation Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Using Beans Call to get. Payment() method Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Java API Bean Properties w. Many Java API methods conform to simple property design patterns w. Can usually treat as bean properties Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Tag Libraries w. Wouldn’t it be nicer to write the mortgage app as Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Tag Libraries Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
Tag Libraries w. Place custom tag definition in a tag file having the name of the custom action n mortgage. tagx w. Place tag file in a tag library (e. g. , directory containing tag files) n /WEB-INF/tags w. Add namespace declaration for tag library Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC w. Many web apps are based on the Model. View-Controller (MVC) architecture pattern Model Components View Controller HTTP request HTTP response Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC w. Typical JSP implementation of MVC Model Components (beans, DBMS) Controller (Java servlet) HTTP request View (JSP document) HTTP response Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC w. Forwarding an HTTP request from a servlet to another component: n By URL Ex: /Hello. Counter. jspx n By name Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC w. How does the controller know which component to forward to? n get. Path. Info() value of URL’s can be used n Example: l servlet mapping pattern in web. xml: URL ends with: l get. Path. Info() returns: l Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC w. JSP include action Execute specified component and include its output in place of the include element Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
MVC w. Adding parameters to the request object seen by an included component: request object seen by navbar. jspx will include parameter named current. Page with value home Guy-Vincent Jourdan : : CSI 3140 : : based on Jeffrey C. Jackson’s slides
- Csi 3140
- Csi 3140
- Csi 3140
- Csi 3140
- Csi 3140
- Csi 3140
- Csi 3140
- Csi
- Paper chromatography method
- Is a pizza a heterogeneous mixture
- Biology homology
- Cisc 3140
- Werken onder spanning nen 3140
- 631-828-3110
- Hard customer defined standards
- Law and order vs csi
- Csi computer crime and security survey
- Fonctions techniques
- Least reactive non-metal
- Periodic table separating metals nonmetals and metalloids
- Separating and retaining employees
- Central sulcus separates
- Separating style and content with bilinear models
- Quebec separating from canada pros and cons
- Csi 3120
- Csi3120
- Csi 321
- Csi in project management
- Csi handwriting analysis
- Csi varese
- Csi framework
- Csi-2 to usb
- Csi 3131
- Elg 2911
- Csi2911
- While the csi team is searching the crime scene, _____.
- Csi great lakes region
- Bnl csi
- Csi uottawa
- Csi enterprises inc
- Csi uottawa
- Csi dinner edition answers
- Csi 2132
- Csi 321
- Master csi
- Cara menghitung csi
- Csi xcad
- Csi como 7
- Csi 3531
- Csi2911
- Inference rules
- Csi la molina
- Csi trigger tracker
- Agenda scramble words
- Uds cad
- Katrin csi
- Csi
- A csi lab technician _____.
- Csi 3131
- Csi 2510
- Crime scene search patterns examples
- Epbb calc
- Who was george sanderlin boston massacre
- Amir afrasiabi
- Csi cable jamestown
- Continual service improvement 7 steps
- Csi basics
- Csi bones
- Csi 201
- Funga дэрил
- Csi 201
- Csi 101
- Example clues sentences
- Csi atherectomy setup
- Csi 2510
- Elg 2911
- You couldnt have
- Csi-321 introduction to computing applications
- Csi bible
- Emerson csi 6500
- Csi office on aging
- Csi csif comunidad valenciana enseñanza
- Csi incentive
- Csi bible
- Csi human resources
- Separating dna