1 / 42

JavaServer Page

JavaServer Page. Presented by: Hongmei Yu 04/18/2001. 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. Introduction to JavaServer Page.

azriel
Télécharger la présentation

JavaServer Page

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  3. 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.

  4. 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.

  5. A simple Example The main HTML file(SimpleJSP.html) <HTML> <HEAD> <TITLE>Simple JSP Example</TITLE> </HEAD> <BODY> <P>How many times?</P> <FORM METHOD="GET" ACTION="SimpleJSP.jsp"> <INPUT TYPE="TEXT" SIZE="2" NAME="numtimes"> <INPUT TYPE="SUBMIT"> </FORM> </BODY> </HTML>

  6. A simple Example The response file(SimpleJSP.jsp) <%@ page language="java" %> <HTML> <HEAD> <TITLE>Simple JSP Example</TITLE> </HEAD> <BODY> <P> <% int numTimes = Integer.parseInt(request.getParameter("numtimes")); for (int i = 0; i < numTimes; i++) { %> CSI 668!<BR> <% } %> </P> <%@ include file="PageFooter.html" %> </BODY> </HTML>

  7. JSP directives: Sets attributes for the page 1.page directive --defines a number of important attributes that affect the whole page. < %@ page Attributes %> 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. <%@ include file=“FILENAME” %> 3.taglib directive --Allows the page to use custom user defined tags. <%@ taglib uri=“tagLibraryURI” prefix=“tagPrefix” %>

  8. Scripting Elements—are used to include scripting code (normally java code) within the JSP. 1.Declarations <%! Java variable and method declaration(s) %> 2.Scriptlets <% Java code statement%> 3.Expressions <%= Java expression to be evaluated %>

  9. Standard Actions affect the runtime behavior of the JSP affect the response sent back to the client 1. <jsp:useBean> 2. <jsp:setProperty> 3. <jsp:getProperty> 4.<jsp:param> 5.<jsp:include> 6.<jsp:forward> 7.<jsp:plugin>

  10. Browser Server User enters value into form and clicks submit button Sends requests to server For JSP page, including Data from form Interprets JSP and uses data from form to generate response Sends response to browser Containing HTML code Response displayed in browser window Time

  11. 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.

  12. 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.

  13. 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.)

  14. 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.

  15. Comparison with Existing Technologies ASP JSP ASP Platforms All major web platforms Microsoft only Base Language Java Jscript or VBScript Components JSP Tags, JavaBeans, or COM/DCOM Enterprise JavaBeans Code Interpretation Once Each instance

  16. Comparison with Existing Technologies ASP 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.

  17. Why to use JavaServer Page • Easy and Rapid Web Development,and Maintenance • Emphasizing Reusable Components • Separating Content Generation from Presentation • Platform Independence • Simplifying Page Development with Tags

  18. 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 - <jsp:usebean> • Initializing a Bean - <jsp:setProperty> • Displaying Dynamic Content - <jsp:getProperty>

  19. 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.

  20. 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. <jsp:setProperty name=“ beanName “ propertydetails /> name--- The name of a bean instance defined by a <jsp:bean> tag property—The name of the bean property whose value is being set.

  21. 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. <jsp:getProperty name=“ name” property=“ propertyName” /> name--- The name of the bean instance from which the property is obtained. property--- The name of the property to retrieve.

  22. A example for using JavaBean An HTML form(which obtains input from the user to be processed by our JSP) ,SetProTest.html <html> <head> <title>A example </title> </head> <body bgcolor="#FFFFFF"> <form action="wordpro1.jsp" method="POST"> Enter word: <input type="text" name="word"> <select name="mode"> <option value="1" selected>Reverse</option> <option value="2" selected>SpellCheck</option> </select> <input type="submit" name="Go" value="Submit"> </form> </body> </html>

  23. A example for using JavaBean The main JSP file(wordpro1.jsp) <jsp:useBean id="help" scope="session" class="SpellCheck" /> <jsp:setProperty name="help" property="*"/> <html> <body> You entered the input, <b> <jsp:getProperty name=“help” property=“word”/> </b><br> The processed output is :<br> <%= Integer.parseInt(request.getParameter("mode"))==1 ? help.reverse() :""+help.check() %> </body> </html>

  24. 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(); }

  25. A example for using JavaBean The bean continue(SpellCheck.java) /** * 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; }

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

  27. JSP Session 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. 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.

  28. JSP Session Time HTTP connection Select items from catalog Catalog Transfer the selected items to the order at some point during the session Browsing Order Order and pay for items

  29. 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.

  30. A example of JSP Session The first page(p1.jsp) <HTML> <BODY> <FORM METHOD=POST ACTION="p2.jsp"> Please input your name: <INPUT TYPE=TEXT NAME="thename"> <INPUT TYPE=SUBMIT VALUE="SUBMIT"> </FORM> </BODY> </HTML>

  31. A example of JSP Session The second page(p2.jsp---1) <HTML> <BODY> <%@ page language="java" %> <%! String name=""; %> <% name = request.getParameter("thename"); session.putValue("thename", name); %> Your name is: <%= name%> <p>

  32. A example of JSP Session The second page(p2.jsp—2) <FORM METHOD=POST ACTION="p3.jsp"> What's the topic you like ? <INPUT TYPE=TEXT NAME="topic"> <P> <INPUT TYPE=SUBMIT VALUE="SUBMIT"> </FORM> </BODY> </HTML>

  33. A example of JSP Session The third page(p3.page) <HTML> <BODY> <%@ page language="java" %> <%! String topic=""; %> <% topic = request.getParameter("topic"); String name = (String) session.getValue("thename"); %> Your name is : <%= name%> <P> Your topic is: <%= topic%> </BODY> </HTML>

  34. 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.

  35. Two Basic Ways of Using JSP Technology The Page-Centric Approach Page-View Request Business Processing JSP Response Page-View With Bean Request Business Processing JSP Bean Response

  36. 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: • separation of presentation from content • not be suitable for complex implementation • Does not scale up well for a large number of simultaneous clients

  37. 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 .

  38. 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.

  39. Two Basic Ways of Using JSP Technology The “Dispatcher” Approach 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.

  40. Final Words • Some useful links • http://java.sun.com/products/jsp. • http://archives.java.sun.com/archives/jsp-interest.html • http://www.flashline.com/components/appservermatrix.jsp • http://www.flashline.com/ • http://www.taglib.com/ /com

  41. Thank you !

More Related