Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp2513 Java Server Pages Daniel L. Silver, Ph.D.

Similar presentations


Presentation on theme: "Comp2513 Java Server Pages Daniel L. Silver, Ph.D."— Presentation transcript:

1 Comp2513 Java Server Pages Daniel L. Silver, Ph.D.

2 2001Daniel L. Silver2 Objectives Introduce Java Server Pages and their advantages over Servlets Introduce Java Server Pages and their advantages over Servlets To understand how JSPs work: The JSP Operational Model To understand how JSPs work: The JSP Operational Model To understand the basic components of JSPs To understand the basic components of JSPs To understand how JSPs work with Java Beans To understand how JSPs work with Java Beans Review examples of JSPs Review examples of JSPs –Web Reference: »JSP Intro JSP IntroJSP Intro »Servlet and JSP Programming with IBM Websphere – Ch. 5 Servlet and JSP Programming with IBM WebsphereServlet and JSP Programming with IBM Websphere –Paper Reference: EJP – Chapter 10

3 2001Daniel L. Silver3 The Problems With Java Servlets The presentation code (HTML) and business logic are closely coupled within java servlet The presentation code (HTML) and business logic are closely coupled within java servlet Large amount of source code required to display simple HTML of web-page Large amount of source code required to display simple HTML of web-page If HTML presentation needs to be changed then servlet source code must be modified If HTML presentation needs to be changed then servlet source code must be modified If business logic needs to be changed then it must be found within the mix of HTML and Java If business logic needs to be changed then it must be found within the mix of HTML and Java E-Commerce Web developers are typically not Java programmers and visa versa E-Commerce Web developers are typically not Java programmers and visa versa

4 2001Daniel L. Silver4 JSPs – Java Server Pages JSPs – a relatively new Java programming model from SUN (similar to Netscape Server-side Javascript and MS Active Server Pages, but more powerful) JSPs – a relatively new Java programming model from SUN (similar to Netscape Server-side Javascript and MS Active Server Pages, but more powerful) Developed to separate the dynamic content (Java) of pages from the more static HTML content Developed to separate the dynamic content (Java) of pages from the more static HTML content I.E.: application processing (business logic) code from the HTML presentation code I.E.: application processing (business logic) code from the HTML presentation code Looks like HTML but allows execution of Java* within the HTML file or access to component objects (Java Beans) Looks like HTML but allows execution of Java* within the HTML file or access to component objects (Java Beans) * the specification allows for coding/scripting languages other than Java

5 2001Daniel L. Silver5 Skeleton of a JSP <HTML><HEAD> HelloJSP1 HelloJSP1 </HEAD><BODY> Hello JSP - Example 1 Hello JSP - Example 1 <jsp:useBean id="textProvider" id="textProvider" class="exampleBean.HelloJSP1" class="exampleBean.HelloJSP1" scope="request"> scope="request"></jsp:useBean> </BODY></HTML> Standard HTML Code Java Object Created from a JavaBean Calling a method of the Java Object Special JSP Tags

6 2001Daniel L. Silver6 How useful are JSPs? Web page developers design and create the web-pages, incorporating styles and aesthetics. (Presentation Layer) The dynamic content is left to the Java code. Web page developers design and create the web-pages, incorporating styles and aesthetics. (Presentation Layer) The dynamic content is left to the Java code. The JSP file can call other servlets and perform the business logic features of the site. The Java programmer can concentrate on the functionality (Application Layer) of the web site. The JSP file can call other servlets and perform the business logic features of the site. The Java programmer can concentrate on the functionality (Application Layer) of the web site.

7 2001Daniel L. Silver7 Advantages of JSPs Separation of dynamic and static content Separation of dynamic and static content –Web developer creates and maintains the HTML content –Java programmer creates and maintains dynamic content and business logic Platform independence Platform independence –Standards promote portability (but …beware) Component reuse Component reuse –Promotes the use of JavaBeans and Enterprise JavaBeans –Speeds up website development and support

8 2001Daniel L. Silver8 Advantage of JSPs Scriptlets and Tags Scriptlets and Tags –JSP Scriptlets »Small pieces of Java code embedded directly in HTML –JSP Tags »Provide an easy way to embed and modify JavaBean properties for specific use »Issue JSP directives and other actions

9 2001Daniel L. Silver9 The JSP Operational Model 1. HTTP server receives request for.jsp file 2. The.jsp file is located (can be in any directory) 3. Tomcat is called and passed the.jsp file 4. Tomcat invokes a special servlet called pageCompile (the JSP engine, comes as part of JSP distribution) 5. pageCompile builds a servlet based on JSP code (that may include a reference to a JavaBean) 6. The servlet.java and.class files are written to disk (internal to Tomcat) 7. The.class file is processed by the JVM within Tomcat like any other servlet 8. HTML code is generated by the servlet and returned

10 2001Daniel L. Silver10 Tomcat Java Servlet Request Processing Internet Browser Client HTTP Server HelloWorld.jsp mod_jserv http://eagle.acadiau.ca/store05/HelloWorld.jsp Tomcat App. Server../Store05/HelloWorld.jsp HTML HelloJSP1.class../Store05/WEB-INF/classes/exampleBean/HelloJSP1 HelloWorld.java HelloWorld.class 1 8 2 4 3 6 5 7 pageCompile

11 2001Daniel L. Silver11 Components of JSPs JSP are composed of standard HTML tags and JSP tags JSP are composed of standard HTML tags and JSP tags The standard set of JSP tags can be categorized as follows: The standard set of JSP tags can be categorized as follows: –Comments –Directives –Declarations –Scriptlets –Expressions

12 2001Daniel L. Silver12 JSP Comments HTML comment will appear in the output stream to the browser HTML comment will appear in the output stream to the browser JSP comment will not be processed by the JSP compiler JSP comment will not be processed by the JSP compiler

13 2001Daniel L. Silver13 Directives JSP directive is a global definition for the JSP engine (compiler) that normally appears at the top of the JSP page JSP directive is a global definition for the JSP engine (compiler) that normally appears at the top of the JSP page Page directive Page directive Include directive Include directive

14 2001Daniel L. Silver14 Declarations A declaration block defines Java variables and methods that are accessible to other code within the page (such as an expression block) A declaration block defines Java variables and methods that are accessible to other code within the page (such as an expression block) Example: Example:<%! private int getDateCount = 0; private String getDate(GergorianCalendar gcl) { …. method code …}; %>

15 2001Daniel L. Silver15 Expressions An expression is a scriptlet fragment that produces a result & emits it as String object. An expression is a scriptlet fragment that produces a result & emits it as String object. Used to generate dynamic text, prevents having to use println() statements Used to generate dynamic text, prevents having to use println() statements Example: Example:

16 2001Daniel L. Silver16 JSP Scriptlets Java code fragments can be embedded within a JSP to provide small pieces of logic that dynamically manipulate HTML and do not warrant creation of a JavaBean Java code fragments can be embedded within a JSP to provide small pieces of logic that dynamically manipulate HTML and do not warrant creation of a JavaBean Example: Example: <% if (Calendar_Time.AM_PM== Calendar.AM) { %> { %> How are you this morning, How are you this morning, How are you this afternoon, How are you this afternoon,

17 2001Daniel L. Silver17 Access to Implicit Objects When writing scriptlets or expressions there is often a need to access standard Java objects: When writing scriptlets or expressions there is often a need to access standard Java objects: –request - access to request object »Example of use: request.getParemeter() –response – access to response object »Example of use: response.setHeader() –out – output stream writer »Example of use: out.println()

18 2001Daniel L. Silver18 Putting it all Together DateDisplay is a basic JSP that you can begin to learn from DateDisplay is a basic JSP that you can begin to learn from DateDisplay is DateDisplay is Here is a link to the JSP source (view in source mode) Here is a link to the JSP source (view in source mode)JSP sourceJSP source Here is a link to the Java source resulting from the JSP compilation Here is a link to the Java source resulting from the JSP compilationJava sourceJava source

19 THE END danny.silver@acadiau.ca


Download ppt "Comp2513 Java Server Pages Daniel L. Silver, Ph.D."

Similar presentations


Ads by Google