Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.

Similar presentations


Presentation on theme: "Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped."— Presentation transcript:

1 Basic JSP Celsina Bignoli bignolic@smccd.net

2 Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped together! Problems: –good Java knowledge is needed to develop and maintain the application –changing look and feel or a new client type require changes to the Servlet code –cannot use web page development tools

3 Advantages of JSP separates request processing and business logic from the presentation –places all static HTML in a JSP page and adds a few JSP elements to generate the dynamic content allows the separation of tasks between Java programmers and WEB page authors makes it easier to change presentation without affecting business layer and viceversa

4 A Simple JSP Page Static: HTML/XML elements Dynamic: scriptlets Special JSP elements Example.jsp <% Date d=new Date(); String today = DateFormat.getInstance().format(d); %> Today is: HTML Scriptlet JSP tag

5 JSP Processing- Translation Phase Servlet Container JSP Page (.jsp) JSP Page (.jsp) Servlet Source (.java) Servlet Source (.java) JSP Translator Servlet Class (.class ) Servlet Class (.class ) Java Compiler JVM Translation Phase Request Processing Phase Text Buffer Text Buffer requestresponse

6 JSP Processing- Request Processing Phase Servlet Container JSP Page (.jsp) JSP Page (.jsp) Servlet Source (.java) Servlet Source (.java) JSP Translator Servlet Class (.class ) Servlet Class (.class ) Java Compiler JVM Translation Phase Request Processing Phase Text Buffer Text Buffer requestresponse

7 package jsp; import javax.servlet.*; import javax.servlet.http.*; … import java.text.*; import java.util.*; public class example_jsp extends org.apache.jasper.runtime.HttpJspBase { … } example_jsp.java extends: javax.servlet.http.HttpServlet JSP Translation - Example

8 public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { … try { out = pageContext.getOut(); out.write(" \r\n"); Date d=new Date(); String today = DateFormat.getInstance().format(d); out.write("\r\n"); out.write("Today is:\r\n"); out.write(" "); out.print(today); out.write(" \r\n"); out.write(" "); } … } service() method - Example

9 JSP Elements Directives Comments Actions –standard actions –custom actions and JSTL Expression Language (EL) Scripting JavaBean components

10 JSP Directive Elements Specify attribute of the page itself –type of content –buffering requirements –resources used –how runtime errors should be handled Do not affect content of a response but how the container should handle it. Enclosed between Have a name and one or more attribute/value pairs –attribute and values are case-sensitive –values must be enclosed in single or double quotes

11 Page Directive other possible values for contentType –text/plain –text/xml –text/vnd.wap.wml other possible attributes –errorPage –isErrorPage –session –pageEncoding –buffer –autoFlush

12 Taglib Directive declares a JSTL custom tag library used in the page the prefix is the name used for the library in the JSP page the uri uniquely identifies the library

13 JSP Comments delimited by ignored when the page is processed never sent to the browser

14 JSP Actions represent dynamic actions to be performed at runtime action_body or allows dor actions in different libraries to have the same name allow contaienr to determine which library an action belongs

15 Standard Actions makes a java bean component available to a page gets a property value from a JavaBean components and adds it to the response Set a JavaBean property value Include the response from a servlet or JSP page during the request processing phase Forwards the processing of a request to a servlet or JSP page

16 Standard Actions (2) adds a parameter to a request handed over to another servlet or JSP page Used to run applets on the browser Set the value of an action attribute Sets the action element body Dynamically generate an XML element to encapsulate text “verbatim”

17 JSP Standard Tag Library (JSTL) group of libraries each containing related actions Core XML processing Internationalization Relational Database Access Functions

18 JSP Expression Language based on both ECMAScript and XPath built-in support for JavaBean access and manipulation, collection of objects, automatic type conversion etc… EL expressions enclosed within ${ and } ${anObject.aProperty} 10000}”> …

19 Scripting Elements fragments of java code embedded in a JSP page Declarations Expressions Scriptlets heavily used in early JSP pages but rarely in newer developments (and discouraged)

20 Declarations used to insert methods, constants and variable declarations in JSP pages <%! private static String EXAMPLE=“/example2”; private static String SHOP_PAGE=“/estore.jsp”; private static String CART_PAGE=“/shopcart.jsp”; private String dispPrice(String price) { // method body } %>

21 Expressions an embedded Java expression that is evaluated and converted to a text String The text string is placed in the JSP output at the location in which the element appears

22 Scriptlets used to include complete fragments of java code in a JSP page cau use the out implicit object (of type javax.servlet.jsp.JspWriter) to write output <% if (state.isLocal()) out.print(totalCost * localTax) else out.print(totalCost) %>

23 Problems with Scriptlets discouraged since they do not promote separation of presentation from data/logic look as a Servlet inside-out make JSP page as hard to write/maintain as corresponding Servlet

24 Model-View-Controller Design Separation of –data and business logic (Model) –data presentation (View) –data interaction (Controller) The user interacts with the Controller to ask for things to be done. Controller forward the request to the Model the result of the request is displayed by the View

25 Model-View-Controller Design Controller (Servlet) View (JSP) Model (JavaBean) Data Data Tier JSP/Servlet Container Request Response Browser


Download ppt "Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped."

Similar presentations


Ads by Google