Presentation is loading. Please wait.

Presentation is loading. Please wait.

04/18/20011 JavaServer Page Presented by: Hongmei Yu 04/18/2001.

Similar presentations


Presentation on theme: "04/18/20011 JavaServer Page Presented by: Hongmei Yu 04/18/2001."— Presentation transcript:

1

2 04/18/20011 JavaServer Page Presented by: Hongmei Yu 04/18/2001

3 2 JavaServer Page What is a JavaServer Page? Why to Use JavaServer Page JSP and JavaBeans JSP Session Two Basic Ways of Using JSP Tecnology Final words

4 04/18/20013 Introduction to JavaServer Page JavaServer Pages are text files that combine standard HTML and new scripting tags. JSPs are look like HTML, but they get compiled into Java servlets the first time they are invoked. The resulting servlet is a combination of the HTML from the JSP file and embedded dynamic content specified by the new tags.

5 04/18/20014 What is a JavaServer Page Definition: JSP is a dynamic capability for web pages that allows java as well as a few special tags to be embedded into a web file(HTML/XML, etc). The suffix traditionally ends with.jsp to indicate to the web server that the file is a JSP file. JSP is a server side technology.

6 04/18/20015 A simple Example Simple JSP Example How many times? The main HTML file(SimpleJSP.html)

7 04/18/20016 A simple Example Simple JSP Example <% int numTimes = Integer.parseInt(request.getParameter("numtimes")); for (int i = 0; i < numTimes; i++) { %> CSI 668! <% } %> The response file(SimpleJSP.jsp)

8 04/18/20017 JSP directives: Sets attributes for the page 1.page directive --defines a number of important attributes that affect the whole page. 2.include directive --notifies the container to include the content of the resource in the current JSP.The included file is parsed by the JSP and this happens only at translation time. 3.taglib directive --Allows the page to use custom user defined tags. <%@ taglib uri=“tagLibraryURI” prefix=“tagPrefix” %>

9 04/18/20018 Scripting Elements —are used to include scripting code (normally java code) within the JSP. 1.Declarations 2.Scriptlets 3.Expressions

10 04/18/20019 Standard Actions affect the runtime behavior of the JSP affect the response sent back to the client 1. 2. 3. 4. 5. 6. 7.

11 04/18/200110 BrowserServer User enters value into form and clicks submit button Response displayed in browser window Interprets JSP and uses data from form to generate response Time Sends requests to server For JSP page, including Data from form Sends response to browser Containing HTML code

12 04/18/200111

13 04/18/200112 Comparison with Existing Technologies CGI(Common Gateway Interface) –The simplest and oldest way to use an HTTP request to control the HTTP output of a server-side application. –Required loading/unloading of application each time. –Used interpreted languages usually(PERL, C++, or Python). By comparison: 1.JSP can maintain state on the server between requests(since it can use Servlet sessions). 2.Spawns a new thread for each request. 3.Does not have to be loaded each time, once it has been initiated. 4.Runs in a ready-loaded JVM as an extension to the web server.

14 04/18/200113 Comparison with Existing Technologies Web Server APIs --Many web servers have their own built-in APIs for creating dynamic content. ISAPI for Microsoft IIS NSAPI for Netscape web servers --When using a server API, the code is written specifically for that web server and particular platform.

15 04/18/200114 Comparison with Existing Technologies Client_Side Scripting(JavaScript, Jscript, VBScript) Vary interpretations by different browser versions. A user can also decide to disable scripting altogether. Since JSP runs on the server as such, the browser is not an issue(unless the JSP products HTML code which contains client side scripting code.)

16 04/18/200115 Comparison with Existing Technologies Servlets Servlets provide the ability to build dynamic content for web sites using the Java language, and are supported on all major web server platforms. The JSP specification is built on top of the Java Servlet API.Anything that can be done with a JSP can be done with a Servlet. JSPs provide a much cleaner separation of presentation from logic, and are simpler to write. Servlets and JSP are work well together.

17 04/18/200116 JSP ASP Comparison with Existing Technologies ASP Platforms All major web platformsMicrosoft only Base Language Java Jscript or VBScript Components JSP Tags, JavaBeans, or COM/DCOM Enterprise JavaBeans Code InterpretationOnce Each instance

18 04/18/200117 Comparison with Existing Technologies JSPs score over ASP in that: JSPs are interpreted only once, to Java byte-code, and re-interpreted only when the file is modified. JSPs run on all the main web servers JSPs provide better facilities for separation of page code and template data by means of JavaBeans, Enterprise JavaBeans and custom tag libraries. ASP

19 04/18/200118 Why to use JavaServer Page Easy and Rapid Web Development,and Maintenance Easy and Rapid Web Development,and Maintenance Emphasizing Reusable Components Emphasizing Reusable Components Separating Content Generation from Presentation Separating Content Generation from Presentation Platform Independence Platform Independence Simplifying Page Development with Tags Simplifying Page Development with Tags

20 04/18/200119 JSP and JavaBeans  The JavaBeans specification allows software components to be written in Java. This means reusable bean classes can be defined, greatly reducing future maintaince requirements.  A property of a JavaBean is simply the data (state) of the bean. Properties are accessible by two methods: the getter and the setter. The value of the property is accessed by the getter method. If the property is writeable, its value is changed by the setter method. Loading a Bean - Initializing a Bean - Displaying Dynamic Content -

21 04/18/200120 JSP and JavaBeans JSP:useBean action is used to associate a JavaBean with the JSP. <jsp: useBean id=“name” scope=“page| request| session| application” beandetails/> class=“className” Class=“className” type=“typeName” Type=“typeName” Id---the case sensitive name used to identify the object instace. Scope—The scope within which the reference is available. The default value is page.

22 04/18/200121 JSP and JavaBeans Jsp:setProperty ----- is used in conjunction with the useBean action described in the preceding section, and sets the value of simple and indexed properties in a bean. name--- The name of a bean instance defined by a tag property—The name of the bean property whose value is being set.

23 04/18/200122 JSP and JavaBeans jsp: getProperty --- is complementary to the jsp:setProperty action and is used to access the properties of a bean. It access the value of a property, converts it to a String, and prints it to the output stream. name--- The name of the bean instance from which the property is obtained. property--- The name of the property to retrieve.

24 04/18/200123 A example for using JavaBean An HTML form(which obtains input from the user to be processed by our JSP),SetProTest.html A example Enter word: Reverse SpellCheck

25 04/18/200124 A example for using JavaBean The main JSP file(wordpro1.jsp) You entered the input, The processed output is :

26 04/18/200125 A example for using JavaBean The bean(SpellCheck.java) /*** This bean encapsulatesthe functionality to spell check a string */ public class SpellCheck { private String word; public SpellCheck() {} /** * Method to reverse the string uses * @return the reversed string */ public String reverse(){ return (new StringBuffer(word).reverse()).toString(); }

27 04/18/200126 /** * Check the spelling of the word. This method has ano body, and just * returns true for the example * @return boolean, true if the spelling is right */ public boolean check(){ return true; } /** * Access method for the word property * @return the current value of the word property */ public String getWord() { return word; } A example for using JavaBean The bean continue(SpellCheck.java)

28 04/18/200127 /** * Sets the value of the word property * @param aWord the new value of the word property */ public void setWord(String aWord) { word = aWord; } A example for using JavaBean The bean continue(SpellCheck.java)

29 04/18/200128 What’s a session? A session can be defined as a series of related interactions between a single client and server, which take place over a period of time. JSP Session For example, consider a common three-step user interaction with a web site: 1.The user browses catalog on a web site, and chooses something from it to putting the shopping basket. 2. The user continues to use other tools and utilities on the site. 3.The user decides to complete the order selected in the first step.

30 04/18/200129 JSP Session Catalog Order Transfer the selected items to the order at some point during the session Time Order and pay for items HTTP connection Select items from catalog Browsing

31 04/18/200130 JSP Session Session Lifecycle It is created on the server as a result of a request and assigned a unique session ID and this ID is then passed to the client. The session itself, is however considered ‘new’ until the client return the session ID to the server indicating that a session has been established. This associated the client with that particular session object. A session exits on the server until it is invalidated(client logout, for example) or the server is stopped.

32 04/18/200131 A example of JSP Session The first page(p1.jsp) Please input your name:

33 04/18/200132 A example of JSP Session The second page(p2.jsp---1) <% name = request.getParameter("thename"); session.putValue("thename", name); %> Your name is:

34 04/18/200133 What's the topic you like ? A example of JSP Session The second page(p2.jsp—2)

35 04/18/200134 A example of JSP Session The third page(p3.page) <% topic = request.getParameter("topic"); String name = (String) session.getValue("thename"); %> Your name is : Your topic is:

36 04/18/200135 Two Basic Ways of Using JSP Technology The Page-Centric Approach This model allows JSPs or Servlets direct access to some resource like a database or legacy application to service a client’s request. The JSP page is where the incoming request is intercepted processed, and the response sent back to the client.

37 04/18/200136 Two Basic Ways of Using JSP Technology The Page-Centric Approach Page-View JSP Business Processing Request Response Page-View With Bean JSP Business Processing Bean Request Response

38 04/18/200137 Two Basic Ways of Using JSP Technology The Page-Centric Approach Advantage: It is simple to program, and allows the page author to generate dynamic content easily, based upon the request and the state of the resources. Disadvantage: l separation of presentation from content l not be suitable for complex implementation l Does not scale up well for a large number of simultaneous clients

39 04/18/200138 Two Basic Ways of Using JSP Technology The “Dispatcher” Approach(or “N-tiered” approach) ------ A SERVLET( or JSP) acts as a mediator or controller, delegating requests to JSP pages and JavaBeans. The application is composed of multiple tiers, the JSP, interacts with the back end resource via another object or Enterprise JavaBeans component. The Enterprise JavaBeans server and the EJB provide managed access to resources, support transactions and access to underlying security mechanisms, thus addressing the resource sharing.

40 04/18/200139 Two Basic Ways of Using JSP Technology The Steps in “N-tiered” application design: Step1—identify the correct objects and their interaction. Step2—identify the JSPs and Servlets. These should be divided into two categories, depending on the role they play. ---Front end JSPs or Servlets that manage application flow and business logic evaluation. This is where a lot of method invocation on the objects, or usage of EJBs, can be coded. They are not responsible for any presentation, and act as a point to intercept the HTTP requests coming from users. ---Presentation JSPs that generate HTML or XML, with their main purpose in life being presentation of dynamic content. The figure below shows the relationship between these two categories.

41 04/18/200140 Two Basic Ways of Using JSP Technology The front-end component accepts a request, and then determines the appropriate presentation component to forward it to. The presentation component then processes the request and returns the response to another front component or directly back to the user. The “Dispatcher” Approach

42 04/18/200141 Final Words Some useful links t http://java.sun.com/products/jsp. http://java.sun.com/products/jsp t http://archives.java.sun.com/archives/jsp-interest.html http://archives.java.sun.com/archives/jsp-interest.html t http://www.flashline.com/components/appservermatrix.jsp http://www.flashline.com/components/appservermatrix.jsp t http://www.flashline.com/ http://www.flashline.com/ t http://www.taglib.com/ /com http://www.taglib.com/

43 04/18/200142 Thank you !


Download ppt "04/18/20011 JavaServer Page Presented by: Hongmei Yu 04/18/2001."

Similar presentations


Ads by Google