CS520 Web Programming Servlet and JSP Review

Slides:



Advertisements
Similar presentations
Expression Language Lec Umair Javed©2006 Generating Dynamic Contents Technologies available  Servlets  JSP  JavaBeans  Custom Tags  Expression.
Advertisements

 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
DT211/3 Internet Application Development JSP: Processing User input.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
DT228/3 Web Development multi page applications/ sharing data.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
CS320 Web and Internet Programming Handling HTTP Requests Chengyu Sun California State University, Los Angeles.
DT211/3 Internet Application Development
CS320 Web and Internet Programming JSP Standard Tag Library (JSTL) Chengyu Sun California State University, Los Angeles.
DT228/3 Web Development JSP: Directives and Scripting elements.
Web programming for project students Dr Jim Briggs.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Intermediate JSP Matt Wheeler. Notes This is a training NOT a presentation If you have questions please ask them Prerequisites – Introduction to Java.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
CS320 Web and Internet Programming Handling HTTP Requests Chengyu Sun California State University, Los Angeles.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Fall CIS 764 Database Systems Design L8. Web ….
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
JSP Expression Language (EL) 25-Nov-15. JSP - E XPRESSION L ANGUAGE (EL) Introduction Expression Language was first introduced in JSTL 1.0 (JSP Standard.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
EL and JSTL. JSTL Sources
OOSSE Week 8 JSP models Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add.
1 Web Programming with Servlets & JSP ASSIGNMENT GUIDELINE.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Intermediate JSP Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic Java.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
CS320 Web and Internet Programming Web Application and MVC Chengyu Sun California State University, Los Angeles.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
National College of Science & Information Technology.
CS320 Web and Internet Programming Handling HTTP Requests Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Introduction to Java Servlets
JSP: Actions elements and JSTL
WWW and HTTP King Fahd University of Petroleum & Minerals
Building Web Apps with Servlets
Web Basics: HTML and HTTP
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
1993 version of Mosaic browser.
CS520 Web Programming Servlet and JSP Review
CS3220 Web and Internet Programming JSP Standard Tag Library (JSTL)
CS520 Web Programming Servlet and JSP Review
Java Servlets.
CS320 Web and Internet Programming JSP Standard Tag Library (JSTL)
Web Software Model CS 4640 Programming Languages for Web Applications
Knowledge Byte In this section, you will learn about:
Chapter 27 WWW and HTTP.
JSP Standard Tag Library
CS320 Web and Internet Programming Cookies and Session Tracking
CS320 Web and Internet Programming Expression Language (EL)
CS320 Web and Internet Programming Java Beans
CS320 Web and Internet Programming MVC Architecture
CS3220 Web and Internet Programming Cookies and Session Tracking
CS3220 Web and Internet Programming Introduction to Java Servlets
CS3220 Web and Internet Programming Handling HTTP Requests
CS3220 Web and Internet Programming Expression Language (EL)
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Cookies and Session Tracking
CS3220 Web and Internet Programming Expression Language (EL)
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS520 Web Programming Servlet and JSP Review Chengyu Sun California State University, Los Angeles

Deliver Dynamic Content Browser Application Server HTTP request HTTP response input output program

Review Examples Add two numbers Guest Book Login and logout Pizza Order

Example 1: Add Two Numbers Take two integer numbers as request parameters and display the sum

Topics Reviewed in Example 1 Web project Servlet @WebServlet Request parameter HTML form GET and POST Deployment

Create a Web Project Eclipse http://csns.calstatela.edu/wiki/content/cysun/course_materials/cs520/development Dynamic Web Project web.xml

Versions The version attribute of <web-app> in web.xml Servlet/JSP Spec Tomcat Java 3.1/2.3 8.0.x 1.7 3.0/2.2 7.0.x 1.6 2.5/2.1 6.0.x 1.5 2.4/2.0 5.5.x 1.4 The version attribute of <web-app> in web.xml

Directory Structure of a Java Web Application Application Root Directory JSPs and static resources WEB-INF web.xml classes Compiled Java classes lib Additional Java libraries

Directory Structure of an Eclipse Dynamic Web Project Application Root Directory WebContent JSPs and static resources WEB-INF web.xml classes build/classes Compiled Java classes lib Additional Java libraries

@WebServlet http://download.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html

@WebServlet Elements for URL Patterns value URL pattern(s) of the servlet The default element urlPatterns Same purpose as value Usually used when more than one element is specified Only one of value and urlPatterns can be specified

@WebServlet Examples @WebServlet( “/HelloServlet” ) @WebServlet( {“/HelloServlet”, “/member/*”} ) @WebServlet( name=“Hello”, urlPatterns={“/HelloServlet”, “/*.html”} ) @WebServlet( urlPatterns=”/MyPattern”, initParams={@WebInitParam(name="ccc", value="333")} )

Wildcard in Servlet Mapping A string beginning with a / and ending with a /* E.g. /*, /content/* A string beginning with a *. E.g. *.html, *.do See Servlet Specification 3.0, Section 12

HTTP Request Example http://cs3.calstatela.edu:8080/whatever GET /whatever HTTP/1.1 Host: cs3.calstatela.edu:4040 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.3) ... Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,... Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cookie: nxt/gateway.dll/uid=4B4CF072; SITESERVER=ID=f1675...

HTTP Request Request line Header [Message body] Method Request URI Protocol Header [Message body]

Request Methods Actions to be performed regarding the resource identified by the Request URI Browser GET POST Editor PUT DELETE Diagnosis HEAD OPTIONS TRACE

Deploy a Web Project on CS3 Understand directory structure “touch” or re-upload web.xml to force Tomcat to reload your application

Directory Structure on CS3 Application Root Directory www JSPs and static resources WEB-INF web.xml classes Compiled Java classes lib Additional Java libraries

Example 2: Guest Book My Guest Book My Guest Book – Add Comment John says: Hello! Edit | Delete Jane says: Your website looks nice. Edit | Delete Joe says: Nice to meet you. I’m from China. Edit | Delete Add Comment My Guest Book – Add Comment Your name: Submit

Topics Reviewed in Example 2 MVC Scopes Expression Language JSTL

Web Application Web application = Data + Operations Data Operations Guestbook entries, blog entries, forum posts, wiki pages, twitter message … Operations Add/create, search, display, edit, delete …

MVC Architecture model 2 3 controller 1 4 browser view 5 Client Server

About MVC Models represent data in the application Controllers implement the actions Handle user input Access and process data Implement business logic Pass data to views for display Views render the display to the users

MVC Using Servlet and JSP Model: Bean (a.k.a. POJO) Controller: Servlet View: JSP HTML, CSS, JavaScript Expression Language (EL) Custom tags (e.g. JSTL) No scripting elements <%! %> <%= %> <% %>

Understand Bean Properties Bean properties are defined by getters and/or setters E.g. getFoo()  foo Read-only: only getter Write-only: only setter Read-write: both getter and setter For a boolean property, the getter starts with is instead of get E.g. isFoo() instead of getFoo()

Scopes and Data Sharing Application scope – data is valid throughout the life cycle of the web application Session scope – data is valid throughout the session redirect, multiple separate requests Request scope – data is valid throughout the processing of the request forward Page scope – data is valid within current page

Common Usage of Scopes Application scope Session scope Request scope Store data shared by all users Session scope Store data associated with a session, e.g. login credentials, shopping cart Request scope Pass data from controller to view Page scope Local variables in a JSP

Access Scoped Variables in Servlet Application scope ServletContext Session scope HttpSession Request scope HttpServletRequest Page scope (in JSP scriptlet) pageContext

Expression Language Expression Language (EL) EL Syntax A JSP 2.0 standard feature A more concise way to write JSP expressions vs. <%= expression %> Java’s answer to scripting languages EL Syntax ${ expression }

Expression Literals Operators Variables Functions see Custom Tag Libraries

EL Operators Conditional empty Other Arithmetic ? : Logical +, -, *, /, % div, mod Logical &&, ||, ! and, or, not Relational ==, !=, <, >, <=, >= eq, ne, lt, gt, le, ge Conditional ? : empty check whether a value is null or empty Other [], ., ()

Implicit Objects pageScope requestScope sessionScope applicationScope pageContext servletContext session request response param, paramValues header,headerValues cookie initParam pageScope requestScope sessionScope applicationScope

Common Usage of EL Access scoped variables ${applicationScope.foo} ${sessionScope.foo} ${requestScope.foo} ${pageScope.foo} ${foo} Access object properties, e.g. ${foo.bar} Simple operations, e.g. ${not empty param.foo}

JSP Standard Tag Library (JSTL) URI Prefix Core http://java.sun.com/jsp/jstl/core c XML Processing http://java.sun.com/jsp/jstl/xml x I18N Formatting http://java.sun.com/jsp/jstl/fmt fmt Database Access http://java.sun.com/jsp/jstl/sql sql Functions http://java.sun.com/jsp/jstl/functions fn http://csns.calstatela.edu/file/view?id=4514026

JSTL Core Flow control Variable support URL Output Exception handling <c:if> <c:choose> <c:when> <c:otherwise> <c:forEach> <c:forToken> Variable support <c:set> <c:remove> URL <c:param> <c:redirect> <c:import> <c:url> Output <c:out> Exception handling <c:catch>

Format Date and Time <fmt:formatDate value=“${date}” type=“date” /> <fmt:formatDate value=“${date}” type=“time” /> <fmt:formatDate value=“${date}” type=“both” /> <fmt:formatDate value=“${date}” pattern=“yyyy-MM-dd hh:mm:ss a” /> See http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html for the date formatting patterns.

JSTL Functions fn:length() fn:contains() fn:containsIgnoreCase() fn:startWith() fn:endsWith() fn:indexOf() fn:replace() fn:trim() fn:toUpperCase() fn:toLowerCase() fn:substring() fn:substringAfter() fn:substringBefore() fn:split() fn:join() fn:escapeXML()

Example 3: Login and Logout A user must login before he or she can add comments to the guest book. My Guest Book John says: Hello! Edit Jane says: Your website looks nice. Edit Login Joe says: Nice to meet you. I’m from China. Edit Add Comment Logout

Topics Reviewed in Example 3 Session tracking Basic login/logout mechanism

Example 4: Pizza Order

Create Your Own Pizza Crusts Cheese Toppings ($1 each) Large Original, $11 Medium Original, $9 Large Thin, $11 Cheese Normal cheese, no cheese (-$1) Toppings ($1 each) Pepperoni, sausage, bacon, pineapple

UI – Customize Pizza Crust: Cheese: Toppings: Large Original $11 Normal No cheese Toppings: Pepperoni Sausage Bacon Pineapple Add to Order

UI – Review Order Large Original Crust, Normal Cheese, Pizza Quantity Price Large Original Crust, Normal Cheese, with Pepperoni, Bacon 1 $13 Large Thin Crust, No Cheese, with Pineapple 2 $22 Total $35 Add Another Pizza Update Order Place Order

Summary Server-side Programming ASP, PHP Servlet … JSP Bean Scripting Elements Bean (Data Modeling) EL (Property Access) Tag Library (Display Logic) Filter web.xml Static content Other libraries Java Web Application