JavaServer Page

  • View

  • Download

Embed Size (px)


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. - PowerPoint PPT Presentation

Text of JavaServer Page

  • JavaServer PagePresented by: Hongmei Yu 04/18/2001

  • JavaServer PageWhat is a JavaServer Page?Why to Use JavaServer PageJSP and JavaBeansJSP SessionTwo Basic Ways of Using JSP Tecnology Final words

  • Introduction to JavaServer PageJavaServer 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.

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

  • A simple Example

    Simple JSP Example

    How many times?

    The main HTML file(SimpleJSP.html)

  • A simple Example

    Simple JSP Example

    CSI 668!

    The response file(SimpleJSP.jsp)

  • JSP directives: Sets attributes for the 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. 3.taglib directive --Allows the page to use custom user defined tags.

  • Scripting Elementsare used to include scripting code (normally java code) within the JSP. 1.Declarations 2.Scriptlets 3.Expressions

  • Standard Actions affect the runtime behavior of the JSP affect the response sent back to the client 1. 2. 3. 4. 5. 6. 7.

  • BrowserServerUser enters value into form and clicks submit buttonResponse displayed in browser windowInterprets JSP and usesdata from form to generate response TimeSends requests to serverFor JSP page, includingData from formSends response to browserContaining HTML code

  • Comparison with Existing TechnologiesCGI(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.

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

  • Comparison with Existing TechnologiesClient_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.)

  • Comparison with Existing TechnologiesServletsServlets 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.

  • JSP ASPComparison with Existing Technologies

    ASPPlatforms All major web platformsMicrosoft onlyBase Language Java Jscript or VBScriptComponents JSP Tags, JavaBeans, or COM/DCOM Enterprise JavaBeansCode InterpretationOnce Each instance

  • 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 serversJSPs provide better facilities for separation of page code and template data by means of JavaBeans, Enterprise JavaBeans and custom tag libraries.ASP

  • Why to use JavaServer Page Easy and Rapid Web Development,and MaintenanceEmphasizing Reusable ComponentsSeparating Content Generation from PresentationPlatform IndependenceSimplifying Page Development with Tags

  • JSP and JavaBeansThe 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 -

  • JSP and JavaBeansJSP:useBean action is used to associate a JavaBean with the JSP. class=classNameClass=className type=typeNameType=typeName

    Id---the case sensitive name used to identify the object instace.ScopeThe scope within which the reference is available. The default value is page.

  • JSP and JavaBeansJsp: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 tagpropertyThe name of the bean property whose value is being set.

  • JSP and JavaBeansjsp: 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 The name of the property to retrieve.

  • A example for using JavaBeanAn HTML form(which obtains input from the user to be processed by our JSP) ,SetProTest.html

    A example

    Enter word:

    Reverse SpellCheck

  • A example for using JavaBeanThe main JSP file(wordpro1.jsp)

    You entered the input, The processed output is :

  • A example for using JavaBeanThe bean(*** 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(); }

  • /** * 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 JavaBeanThe bean continue(

  • /** * 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 JavaBeanThe bean continue(

  • Whats 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 SessionFor 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.

  • JSP SessionCatalogOrderTransfer the selected items to the order at some point during the sessionTimeOrder and pay for itemsHTTP connectionSelect items from catalogBrowsing

  • JSP SessionSession LifecycleIt 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.

  • A example of JSP SessionThe first page(p1.jsp) Please input your name:

  • A example of JSP Session

    The second page(p2.jsp---1)

    Your name is:

  • What's the topic you like ?

    A example of JSP SessionThe second page(p2.jsp2)

  • A example of JSP Session

    The third page(

    Your name is : Your topic is:

  • Two Basic Ways of Using JSP TechnologyThe Page-Centric ApproachThis model allows JSPs or Servlets direct access to some resource like a database or legacy application to service a clients request. The JSP page is where the incoming request is intercepted processed, and the response sent back to the