J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

Java Server Pages (JSP)
Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
JavaServer Pages TM Introduce by
UNIT-V The MVC architecture and Struts Framework.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Server Side Scripting Norman White. Where do we do processing? Client side – Javascript (embed code in html) – Java applets (send java program to run.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
JSP Java Server Pages Softsmith Infotech.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Chapter 7 Being a JSP. JSP introduction JSP is a solution for two issues  Servlet is difficult for HTML designers since they may not know Java  Formatting.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
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.
SKT-SSU IT Training Center Servlet and JSP. Chapter Eleven: Invoking Java Code with JSP Scripting Elements.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
JAVA SERVER PAGES -by Rubeena Memon Deepti Jain Jaya Thakar Jisha Vettuventra.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
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.
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.
Invoking Java Code from JSP
MSIS 655 Advanced Business Applications Programming
JavaServer Faces: The Fundamentals
Presentation transcript:

J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting institutions –Slides not permitted for use in commercial training courses except when taught by coreservlets.com (see Slides can be modified however instructor desires Instructor can give PDF or hardcopy to students, but should protect the PowerPoint files –This slide is suppressed in Slide Show mode

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 2 Invoking Java Code with JSP Scripting Elements

J2EE training: 3 Agenda Static vs. dynamic text Dynamic code and good JSP design JSP expressions Servlets vs. JSP pages for similar tasks JSP scriptlets JSP declarations Predefined variables Comparison of expressions, scriptlets, and declarations

J2EE training: 4 Uses of JSP Constructs Scripting elements calling servlet code directly Scripting elements calling servlet code indirectly (by means of utility classes) Beans Servlet/JSP combo (MVC) MVC with JSP expression language Custom tags Simple Application Complex Application

J2EE training: 5 Design Strategy: Limit Java Code in JSP Pages You have two options –Put 25 lines of Java code directly in the JSP page –Put those 25 lines in a separate Java class and put 1 line in the JSP page that invokes it Why is the second option much better? –Development. You write the separate class in a Java environment (editor or IDE), not an HTML environment –Debugging. If you have syntax errors, you see them immediately at compile time. Simple print statements can be seen. –Testing. You can write a test routine with a loop that does 10,000 tests and reapply it after each change. –Reuse. You can use the same class from multiple pages.

J2EE training: 6 Basic Syntax HTML Text – Blah –Passed through to client. Really turned into servlet code that looks like out.print(" Blah "); HTML Comments – –Same as other HTML: passed through to client JSP Comments – –Not sent to client To get <% in output, use <\%

J2EE training: 7 Types of Scripting Elements Expressions –Format: –Evaluated and inserted into the servlet’s output. I.e., results in something like out.print(expression) Scriptlets –Format: –Inserted verbatim into the servlet’s _jspService method (called by service) Declarations –Format: –Inserted verbatim into the body of the servlet class, outside of any existing methods

J2EE training: 8 JSP Expressions Format – Result –Expression evaluated, converted to String, and placed into HTML page at the place it occurred in JSP page –That is, expression placed in _jspService inside out.print Examples –Current time: –Your hostname: XML-compatible syntax – Java Expression –You cannot mix versions within a single page. You must use XML for entire page if you use jsp:expression.

J2EE training: 9 JSP/Servlet Correspondence Original JSP A Random Number Representative resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println(" A Random Number "); out.println(Math.random());... }

J2EE training: 10 JSP Expressions: Example … JSP Expressions Current time: Server: Session ID: The testParam form parameter:

J2EE training: 11 Predefined Variables request –The HttpServletRequest (1st argument to service/doGet) response –The HttpServletResponse (2nd arg to service/doGet) out –The Writer (a buffered version of type JspWriter) used to send output to the client session –The HttpSession associated with the request (unless disabled with the session attribute of the page directive) application –The ServletContext (for sharing data) as obtained via getServletContext().

J2EE training: 12 Comparing Servlets to JSP: Reading Three Params (Servlet) public class ThreeParams extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { … out.println(docType + " \n" + " "+title + " \n" + " \n" + " " + title + " \n" + " \n" + " param1 : " + request.getParameter("param1") + "\n" + " param2 : " + request.getParameter("param2") + "\n" + " param3 : " + request.getParameter("param3") + "\n" + " \n" + " "); }

J2EE training: 13 Reading Three Params (Servlet): Result

J2EE training: 14 Comparing Servlets to JSP: Reading Three Params (JSP) Reading Three Request Parameters <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> Reading Three Request Parameters param1 : param2 : param3 :

J2EE training: 15 Reading Three Params (Servlet): Result

J2EE training: 16 JSP Scriptlets Format – Result –Code is inserted verbatim into servlet's _jspService Example – XML-compatible syntax – Java Code

J2EE training: 17 JSP/Servlet Correspondence Original JSP foo Representative resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println(" foo "); out.println(bar()); baz();... }

J2EE training: 18 JSP Scriptlets: Example Suppose you want to let end users customize the background color of a page –What is wrong with the following code? ">

J2EE training: 19 JSP Scriptlets: Example Color Testing <% String bgColor = request.getParameter("bgColor"); if ((bgColor == null) || (bgColor.trim().equals(""))) { bgColor = "WHITE"; } %> "> Testing a Background of " "

J2EE training: 20 JSP Scriptlets: Result

J2EE training: 21 Using Scriptlets to Make Parts of the JSP File Conditional Point –Scriplets are inserted into servlet exactly as written –Need not be complete Java expressions –Complete expressions are usually clearer and easier to maintain, however Example – Have a nice day! Have a lousy day! Representative result –if (Math.random() nice day!"); } else { out.println("Have a lousy day!"); }

J2EE training: 22 JSP Declarations Format – Result –Code is inserted verbatim into servlet's class definition, outside of any existing methods Examples – Design consideration –Fields are clearly useful. For methods, it is usually better to define the method in a separate Java class. XML-compatible syntax – Java Code

J2EE training: 23 JSP/Servlet Correspondence Original JSP Some Heading <%! private String randomHeading() { return(" " + Math.random() + " "); } %> (Alternative: make randomHeading a static method in a separate Java class)

J2EE training: 24 JSP/Servlet Correspondence Possible resulting servlet code public class xxxx implements HttpJspPage { private String randomHeading() { return(" " + Math.random() + " "); } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println(" Some Heading "); out.println(randomHeading());... }... }

J2EE training: 25 JSP Declarations: Example JSP Declarations <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> JSP Declarations Accesses to page since server reboot:

J2EE training: 26 JSP Declarations: Result

J2EE training: 27 JSP Declarations: the jspInit and jspDestroy Methods JSP pages, like regular servlets, sometimes want to use init and destroy Problem: the servlet that gets built from the JSP page might already use init and destroy –Overriding them would cause problems. –Thus, it is illegal to use JSP declarations to declare init or destroy. Solution: use jspInit and jspDestroy. –The auto-generated servlet is guaranteed to call these methods from init and destroy, but the standard versions of jspInit and jspDestroy are empty (placeholders for you to override).

J2EE training: 28 JSP Declarations and Predefined Variables Problem –The predefined variables (request, response, out, session, etc.) are local to the _jspService method. Thus, they are not available to methods defined by JSP declarations or to methods in helper classes. What can you do about this? Solution: pass them as arguments. E.g. Note that the println method of JspWriter throws IOException – Use “throws IOException” for methods that use println

J2EE training: 29 Comparing Expressions, Scriptlets and Declarations Task 1 –Output a bulleted list of five random ints from 1 to 10. Since the structure of this page is fixed and we use a separate helper class for the randomInt method, JSP expressions are all that is needed. Task 2 –Generate a list of between 1 and 10 entries (selected at random), each of which is a number between 1 and 10. Because the number of entries in the list is dynamic, a JSP scriptlet is needed. Task 3 –Generate a random number on the first request, then show the same number to all users until the server is restarted. Instance variables (fields) are the natural way to accomplish this persistence. Use JSP declarations for this.

J2EE training: 30 Helper Class: RanUtilities package coreservlets; // Always use packages!! /** Simple utility to generate random integers. */ public class RanUtilities { /** A random int from 1 to range (inclusive). */ public static int randomInt(int range) { return(1 + ((int)(Math.random() * range))); } public static void main(String[] args) { int range = 10; try { range = Integer.parseInt(args[0]); } catch(Exception e) { // Array index or number format // Do nothing: range already has default value. } for(int i=0; i<100; i++) { System.out.println(randomInt(range)); }}}

J2EE training: 31 Task 1: JSP Expressions (Code) Random Numbers <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> Random Numbers

J2EE training: 32 Task 1: JSP Expressions (Result)

J2EE training: 33 Task 2: JSP Scriptlets (Code: Version 1) Random List (Version 1) <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> Random List (Version 1) <% int numEntries = coreservlets.RanUtilities.randomInt(10); for(int i=0; i<numEntries; i++) { out.println(" " + coreservlets.RanUtilities.randomInt(10)); } %>

J2EE training: 34 Task 2: JSP Scriptlets (Result: Version 1)

J2EE training: 35 Task 2: JSP Scriptlets (Code: Version 2) Random List (Version 2) <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> Random List (Version 2) <% int numEntries = coreservlets.RanUtilities.randomInt(10); for(int i=0; i<numEntries; i++) { %>

J2EE training: 36 Task 2: JSP Scriptlets (Result: Version 2)

J2EE training: 37 Task 3: JSP Declarations (Code) Semi-Random Number <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> <%! private int randomNum = coreservlets.RanUtilities.randomInt(10); %> Semi-Random Number:

J2EE training: 38 Task 3: JSP Declarations (Result)

J2EE training: 39 Summary JSP Expressions –Format: –Wrapped in out.print and inserted into _jspService JSP Scriptlets –Format: –Inserted verbatim into the servlet’s _jspService method JSP Declarations –Format: –Inserted verbatim into the body of the servlet class Predefined variables –request, response, out, session, application Limit the Java code that is directly in page –Use helper classes, beans, servlet/JSP combo (MVC), JSP expression language, custom tags

JSP, Servlet, Struts, JSF & Java Training: J2EE Books from Sun Press: © 2005 Marty Hall 40 Questions?