Object-Oriented Enterprise Application Development JavaServer Pages.

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.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
Java Server Pages by Jon Pearce. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Object-Oriented Enterprise Application Development Advanced Servlets.
Object-Oriented Enterprise Application Development Introduction to Servlets.
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,
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
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.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
Java Server Pages. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
Introduction to Java Server Pages (JSPs) Robert Thornton.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Jordan Anastasiade. All rights reserved.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
Deploying CFML on J2EE Servers Vince Bonfanti President New Atlanta Communications, LLC.
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.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
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:
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
1 (Server-Side Programming using Java Server Pages) cs
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Java Servlets and Java Server Pages Norman White Stern School of Business.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
Web Technologies Java Beans & JSP By Praveen Kumar G.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
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.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
 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,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
JSP Java Server Pages. Hello, !
Developing JavaServer Pages
Java Server Pages.
J2EE Lecture 1:Servlet and JSP
Presentation transcript:

Object-Oriented Enterprise Application Development JavaServer Pages

Topics During this class we will examine: Anatomy of a JavaServer Page Merging markup with Java Configuring our JSPs with directives Integrating servlets and JSPs

JavaServer Pages

Justification JavaServer Pages (JSPs) are a technology that allow developers to mix static with dynamic content. JavaServer Pages have been successful for two (2) reasons: JSPs are supported across multiple platforms. JSPs give developers access to all Java technologies instead of using a scripting language.

Roles and Responsibilities Although servlets can generate dynamic content, it's often advantageous to separate the business logic in the servlet from the presentation logic. By using JSPs we can have application programmers building servlets while content designers and graphic artists construct the JSPs.

Versions We'll use version 1.1 of the JavaServer Pages specification. The 1.0 and 1.1 version JSPs are almost totally incompatible with the 0.92 version of the specification.

JSP Structure

Basic Concepts At their heart, basic JSPs look just like regular HTML markup. A static HTML document can be turned into a JSP simply by changing its extension to.jsp. The key difference is that JSPs are dynamic. When a JSP is executed, the JSP engine converts that JSP into an equivalent servlet which then follows the normal lifecycle.

Sample Code - HelloWorld HelloWorld JSP Hello, World!

Dynamic Content If all we needed was static content, then we'd just use HTML. The strength of JSPs lie in their ability to generate and manipulate dynamic content. There are three common ways of performing this generation using JSPs: Expressions, Scriptlets Declarations

Lifecycle Even though JSPs are ultimately compiled into servlets, there are a few differences in their lifecycle. Most of the tags for generating dynamic content are placed within the JSP's jservice() method. This method is called by the JSP's service() method for both GET and POST requests.

JSP Tags

Basic Tags In the 0.92 version of the JSP specification, we identified dynamic content using special tags. In version 1.1 of the JSP specification we can still use some of these tags. We can also use equivalent XML tags.

JSP Expressions The simplest form of dynamic content is the JSP expression. This is used to send a value back to the client. The tag format of an expression is given by:

Sample Code - DynamicHelloWorld DynamicHelloWorld JSP Hello, World! 8. It’s now

Implicit Variables (1 of 3) There are eight (8) implicit variables for each JSP. The four (4) most common are: request : The HttpServletRequest object passed to the JSP. response : The HttpServletResponse object passed to the JSP. session : The client's HttpSession object. out : The PrintWriter object used to send output to the client that initiated the request.

Implicit Variables (2 of 3) Other useful variables include: application : The ServletContext object. config : The ServletConfig object. pageContect : The PageContext object. page : The this object (not currently used).

Implicit Variables (3 of 3) These implicit variable can be used within JSP expressions and scriptlets, but not within JSP declarations.

Sample Code – Status (1 of 2) StatusHelloWorld JSP Hello, World! 8. Current Time: Session ID:

Sample Code – Status (2 of 2) 14. Hostname: Parameter:

JSP Scriptlets Often a single expression isn't adequate for the complexity of the output we want the JSP to generate. JSPs allow us to embed complete segments of Java code within a scriptlet. The tag format of a scriptlet is given by:

Sample Code – Scriptlet (1 of 2) Scriptlet JSP Hello, World! 8. Current Time: Session ID:

Sample Code – Scriptlet (2 of 2) 14. Hostname: <% 18. String testString = request.getParameter("test"); 19. if (null == testString) { 20. testString = "no.such.parameter"; 21. } 22. %> 23. Parameter:

Conditional Tags JSP scriptlets are useful for including conditional content. This cuts down on the amount of data returned to the client. This technique can be used to implement basic security features.

Sample Code – Scriptlet (rev.) (1 of 3) Scriptlet JSP Hello, World! 8. Current Time:

Sample Code – Scriptlet (rev.) (2 of 3) 11. <% 12. if (session.getId() != null) { 13. %> 14. Session ID: <% 18. } 19. %> 20. Hostname:

Sample Code – Scriptlet (rev.) (3 of 3) 23. <% 24. String testString = request.getParameter("test"); 25. if (null == testString) { 26. testString = "no.such.parameter"; 27. } 28. %> 29. Parameter:

Translated Code – Scriptlet (1 of 8) 1.import javax.servlet.*; 2.import javax.servlet.http.*; 3.import javax.servlet.jsp.*; 4.import javax.servlet.jsp.tagext.*; 5.import java.io.PrintWriter; 6.import java.io.IOException; 7.import java.io.FileInputStream; 8.import java.io.ObjectInputStream; 9.import java.util.Vector; 10.import org.apache.jasper.runtime.*; 11.import java.beans.*; 12.import org.apache.jasper.JasperException;

Translated Code – Scriptlet (2 of 8) 13.public class _0002fHtml_0002fscriptlet_0002ejspscriptlet_j sp_0 extends HttpJspBase { 14.static { } 15.public _0002fHtml_0002fscriptlet_0002ejspscriptlet_j sp_0( ) { } 16.private static boolean _jspx_inited = false; 17.public final void _jspx_init() throws JasperException { }

Translated Code – Scriptlet (3 of 8) 18.public void _jspService( HttpServletRequest request, HttpServletResponse response) 19.throws IOException, ServletException { 20. JspFactory _jspxFactory = null; 21. PageContext pageContext = null; 22. HttpSession session = null; 23. ServletContext application = null; 24. ServletConfig config = null; 25. JspWriter out = null; 26. Object page = this; 27. String _value = null;

Translated Code – Scriptlet (4 of 8) 28. try { 29. if (_jspx_inited == false) { 30. _jspx_init(); 31. _jspx_inited = true; 32. } 33. _jspxFactory = JspFactory.getDefaultFactory(); 34. response.setContentType( 35. "text/html;charset=8859_1"); 36. pageContext = _jspxFactory.getPageContext( this, request, response, "", true, 8192, true);

Translated Code – Scriptlet (5 of 8) 37. application = pageContext.getServletContext(); 38. config = pageContext.getServletConfig(); 39. session = pageContext.getSession(); 40. out = pageContext.getOut(); 41. out.write(" \r\n \r\n \r\n Scriptlet JSP \r\n \r\n \r\n\t\t\t Hello, World! \t \r\n\t\t\t Current Time: ");

Translated Code – Scriptlet (6 of 8) 42. out.print( new java.util.Date() ); 43. out.write(" \r\n\t\t\t"- \r\n\t\t\t"); 44. if (session.getId() != null) { 45. out.write("\r\n\t\t\t\t "- Session ID: "); 46. out.print( session.getId() ); 47. out.write(" \r\n\t\t\t"); 48. } 49. out.write("\r\n\t\t\t "- Hostname: "); 50. out.print( request.getRemoteHost() ); 51. out.write(" \r\n\t\t\t");

Translated Code – Scriptlet (7 of 8) 52. String testString = request.getParameter("test"); 53. if (null == testString) { 54. testString = "no.such.parameter"; 55. } 56. out.write("\r\n\t\t\t "- Parameter: "); 57. out.print( testString ); 58. out.write(" \r\n\t "- \r\n ");

Translated Code – Scriptlet (8 of 8) 59. } catch (Exception ex) { 60. if (out.getBufferSize() != 0) 61. out.clearBuffer(); 62. pageContext. handlePageException(ex); 63. } finally { 64. out.flush(); 65. _jspxFactory. releasePageContext(pageContext); 66. } 67.} 68.}

JSP Declarations A JSP declaration exists outside of the jservice() method. This means that code in a declaration cannot make use of the implicit variables. This allows us to declare JSP-level variables and methods. The tag format of an expression is given by:

Sample Code – Declaration (1 of 2) Declaration JSP Declarations 8. Current Time: Session ID:

Sample Code – Declaration (2 of 2) 14. Hostname: Accesses to the page since JSP was loaded

JSP Directives

Overview We use directives to customize the JSP's behavior. Each directive affects the structure of the servlet resulting from the JSP's compilation. There are three (3) types of directives: page: Controls the servlet's structure. include: Inserts a file into the servlet class. taglib: Defines custom markup tags.

Page Directives There are many page directives. We'll look at a few of the most common attributes: import isThreadSafe session errorPage We use the directive tag to indicate that we want a directive to take effect:

Import Attribute The import attribute allows a JSP to import class libraries: By default a JSP automatically imports: java.lang.* java.servlet.* java.servlet.http.* java.servlet.jsp.*

Sample Code - Import Import JSP Imports 9. Current Time:

isThreadSafe Attribute The isThreadSafe attribute forces the JSP's servlet to implement the SingleThreadedModel interface: By default a JSP is assumed to be thread safe in the same way that servlets are. If this isn't the case you can: Make it thread-safe using synchronized blocks Set isThreadSafe to false.

Sample Code – IsThreadSafe (1 of 2) 1. 2.<% 3. String userId = "id" + id; 4. out.println("Your id is " + id); 5. id++; 6.%>

Sample Code – IsThreadSafe (2 of 2) 1. 2.<% 3. synchronized (this) { 4. String userId = "id" + id; 5. out.println("Your id is " + id); 6. id++; 7. } 8.%>

Sample Code - IsThreadSafe Single Thread JSP Accesses since JSP was loaded

Session Attribute The session attribute controls whether or not the JSP can access the client's session: By default a JSP participates in the client's session. If this isn't the behavior we want, we can turn it off:

errorPage Attribute The errorPage attribute specifies the URL to which the client will be sent if an unhandled exception is encountered: The exception that caused the problem will be provided to the page via the exception variable.

Sample Code - ErrorPage ErrorPage JSP Session Variable: 9. <%= 10. session. 11. getAttribute("var").toString() 12. %>

Integrating Servlets and JavaServer Pages

Integration JavaServer Pages allow graphic designers to achieve reasonable separation between their roles and the role of the servlet developer. However, since all of these components will be used together in the final application, we need to integrate them.

Request Dispatcher We've already looked at one approach to integrating servlets and JSPs: the RequestDispatcher class. This remains the simplest way of invoking a JSP from a servlet.

Shared Data It isn't uncommon to need to share data between a servlet and a JSP. The servlet may perform all of the business processing necessary to generate the data to be displayed by the JSP. We could use the client's session, but what if the data isn't required beyond a single request-response pair?

Request Attributes (1 of 2) For data that needs to exist only for a single client request, we can use the HttpServletRequest object to store our data. In addition to the parameters passed in by the client, the HttpServletRequest contains attributes which can be set by servlets and JSPs.

Request Attributes (2 of 2) As with session attributes, request attributes are nothing more than a name-value pair. To manipulate these attributes you can use the following methods: public void setAttribute(String, Object) public Object getAttribute(String)

Sample Code – SetData (1 of 3) 1.import java.io.*; 2.import java.util.*; 3.import javax.servlet.*; 4.import javax.servlet.http.*; 5.public class SendData 6.extends HttpServlet { 7.public void doPost(HttpServletRequest rqst, HttpServletResponse resp) 8.throws ServletException, IOException { 9. doGet(rqst, resp); 10.}

Sample Code – SetData (2 of 3) 11.public void doGet(HttpServletRequest rqst, HttpServletResponse resp) 12.throws ServletException, IOException { 13. rqst.setAttribute("parm", "skippy"); 14. chain(rqst, resp, "GetData.jsp"); 15.}

Sample Code – SetData (3 of 3) 16.private void chain(HttpServletRequest rqst, HttpServletResponse resp, String URL) 17.throws ServletException, IOException { 18. RequestDispatcher dispatcher = getServletContext(). getRequestDispatcher(URL); 19. dispatcher.forward(rqst, resp); 20.} 21.}

Sample Code - GetData GetData JSP Get Shared Value 8. The shared value is

Review During this class we have discussed: Anatomy of a JavaServer Page Merging markup with Java Configuring our JSPs with directives Integrating servlets and JSPs

Resources Core Servlets and JavaServer Pages Marty Hall, Prentice-Hall, Inc., ISBN: Java 2 Platform, Enterprise Edition B. Shannon, et al., Addison-Wesley, ISBN:

Coming Attractions Next week we'll add custom tags to our JavaServer Pages. Please read Chapter 14 in your text.