Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.torontocollege.com JavaServer Pages Fundamentals.

Similar presentations


Presentation on theme: "Www.torontocollege.com JavaServer Pages Fundamentals."— Presentation transcript:

1 www.torontocollege.com JavaServer Pages Fundamentals

2 www.torontocollege.com Concepts After completing this module you will understand the: Advantages of JSP technology JSP architecture Life cycle of a JSP page JSP syntax and semantics Role of JavaBean TM components within JSP pages

3 www.torontocollege.com Objectives By the end of this module you will be able to: Manage session-related information from JSP Communicate between JSP pages Process forms with JSP

4 www.torontocollege.com Prerequisites A general familiarity with object-oriented programming concepts and the Java programming language. A general familiarity with HTML tag

5 www.torontocollege.com Introduction JSP pages typically comprise of: Static HTML/XML components. Special JSP tags Optionally, snippets of code written in the Java programming language called "scriptlets."

6 www.torontocollege.com JSP Advantages Write Once Run Anywhere: Dynamic content can be served in a variety of formats: Recommended Web access layer for n-tier architecture Completely leverages the Servlet API: Separation of static from dynamic content

7 www.torontocollege.com Comparing JSP with ASP JavaServer Pages Active Server Pages Web Server Most popular web servers Microsoft IIS Support Platform Platform independent Windows Support Component Relies on reusable, Model cross-platform COM components,Javabean,EJB VBScript and JScript Scripting Java or JavaScript Security Java security model. NT security architecture. Database JDBC ODBC Access Customizable JSP is extensible Cannot use custom tag Tags with custom tag libraries. Libraries. is not extensible.

8 www.torontocollege.com JSPs and Servlets JSP is nothing but a high-level abstraction of servlets. You can do almost anything that can be done with servlets using JSP--but more easily!

9 www.torontocollege.com Exercise 1 Installing and Configuring Tomcat Download Tomcat in Apache Site Unzip and Install it Create your own project publish directory under webapps. Here we call project directory, and create subdirectory under Project directory. See the following figure

10 www.torontocollege.com../webappsProject/ Web-inf/Classes/ Class file Application name/ For example: email/ … JSPs file and other data Meta-info/ Tag library

11 www.torontocollege.com Configure the Tomcat In the server.xml file, add the following lines <Context path="/project" docBase="webapps/project" crossContext="true" debug="0" reloadable="true" trusted="false" >

12 www.torontocollege.com Set up environment Add the following lines In your autoexec.bat or System Properties/Environment Variables window. set JAVA_HOME=c:\jdk1.3 set ANT_HOME=c:\program files\apache group\jakarta- tomcat set Tomcat_Home=c:\program files\apache group\jakarta- tomcat set CLASSPATH=c:\jdk1.3\lib;c:\jdk1.3\lib\tools.jar;c:\ program files\apache group\jakarta-tomcat\lib\servlet.jar

13 www.torontocollege.com Start Tomcat & try email example Tomcat/bin>Tomcat run or startup Copy email’s jsp files and data files into email folder and email’s class files into web-info folder. Launch a web browser and type: http://127.0.01/project/email/email.jsp

14 www.torontocollege.com General Architecture User enters value into form and clicks submit button Response displayed in browser window Interprets JSP and uses data from form to generate Response Request Object Response Object

15 www.torontocollege.com General Architecture(more detail) Web Application Server Client Web Server Plain Documents Servlet Engine Compiled Servlet JSP Engine JSP Documents

16 www.torontocollege.com The Basics of JSP Everything in a JSP page can be broken into 2 categories  Elements that can be processed on the server  Template data

17 www.torontocollege.com Element basics Directives Declarations Scriptlets Expressions Standard actions

18 www.torontocollege.com Directives The page directive The include directive The taglib directive

19 www.torontocollege.com The page Directive Attribute language extends import session …….

20 www.torontocollege.com The page Directive Attribute Description Default language Defines the scripting language to be used. For future use if JSP engine “Java” supports multiple language extends The superclass that the generated class(into which this JSP page is compiled) must extend. This attribute should be used with extreme caution because the engines usually Attribute omitted provide specialized superclass by default with a lot of functionality that the generated classes extend

21 www.torontocollege.com The page Directive Attribute Description Default Import import package and classes Attribute omitted by default Session Specifies if the page participates in an HTTP “true” session Buffer Specifies the buffering model for output stream The default is buffered with to the client. If the value an implementation buffer is none, then no buffering size of not less that 8kb occurs and al output is written directly through to the ServletResponse by a PrintWriter.

22 www.torontocollege.com The page Directive Attribute Description Default autoFlush If “ture”, the output buffer to the is flushed automatically when it is full. If “false”, a runtime exception is raised to indicate buffer overflow. The default is “ture” isThreadSafe Defines the level of thread safety implemented in the page. If “false” then th e JSP processes quenes up client requests sent to the page for processing. It processes them one at a time, in the order they were received. This is the same as implementing the javax.servlet.SingleThreadModel interface in a servlet. Default is “true”

23 www.torontocollege.com The page Directive Attribute Description Default Info Define an informative stirng that can subsequently be obtained from the page’s implementation of Servlet.getServletInfo() method. It is omitted by default IsErrorPage Indicates if the current JSP page is intended to be the the URL target of another JSP pages’s errorPage. If “true”, then the implicit variable exception is available, and refers to the instance of the java.lang.Throwable thrown at runtime by the JSP causing the error. The default is “false”

24 www.torontocollege.com The page Directive Attribute Description Default errorPage Defines a URL to another JSP that is invoked if an unchecked runtime exception is thrown. The page implementation catches the instance of the Throwable object and passes it to the error page processing. See the isErrorPage attribute above contentType Defines the character encoding for the JSP and the MIME type for response of the JSP page.The default is ”text/html”

25 www.torontocollege.com Example of The Page Directive File name: pagedirective.jsp <%@ page language="java" import="java.rmi.*,java.util.*" session="true" buffer="12kb" autoFlush="true" info="my page directive jsp" errorPage="Error.jsp" isErrorPage="false" isThreadSafe="false"%>

26 www.torontocollege.com This is a JSP to test the page directive tag

27 www.torontocollege.com Include Directive The include directive lets you separate you content into more manageable elements, such as those for including a common page header or footer. The page included can be a static HTML page or more JSP content. For example, the directive: can be used to include the contents of the indicated file at any location within the JSP page.

28 www.torontocollege.com The taglib Directive This directive allows the page to use custom user defined tags <%@ taglib uri=“http://www.myserver.com/mytags” prefix=“pooh”http://www.myserver.com/mytags …………..

29 www.torontocollege.com Scripting Elements Declaration Scriptlets Expressions

30 www.torontocollege.com Declarations Example Hello World <%! private int i=4; // my counter public void myMethod(){ // do some work here } %>

31 www.torontocollege.com Scriptlets Example This is a scriptlet example <% for(int i = 0; i < 10; i++){ out.println(" Hello World This is a loop test "+i +" "); System.out.println("This goes to the system.out stream "+i); } %>

32 www.torontocollege.com Expression Example <% i++; %> Hello world !

33 www.torontocollege.com Standard Action

34 www.torontocollege.com Using JavaBean Components The component model for JSP technology is based on JavaBeans component architecture. JavaBeans components are nothing but Java objects which follow a well-defined design/naming pattern: the bean encapsulates its properties by declaring them private and provides public accessor (getter/setter) methods for reading and modifying their values.

35 www.torontocollege.com jsp:useBean For example: Important: What is scope? We will cover it shortly.

36 www.torontocollege.com Beandetails: class=“className” class=“className” type=“typename” beanName=“beanName” type=“typeName”

37 www.torontocollege.com Scope in tag Scope in tag Description page The object exists for every client request to the resource Request The object reference is available as long as the HttpRequest object is not discarded,even if the request is passes/chained to different pages. The object is distinct for every client request Session The object is distinct for every client, and is available as long as the client’s session is valid Application This is not unique for clients, and consequently all clients access the same object.

38 www.torontocollege.com Scope in tag

39 www.torontocollege.com What does the container do under the semantics? The container tries to locate an object that has this id, in the specified scope If the object is found, and a type has been specified in the tag, the container tries to cast the found object to the specified type. A java.lang.ClassCastException is thrown if the cast fails. If the object is not found in the specified scope, and no class or beanName is specified in the tag, a java.lang.InstantiationException is throw

40 www.torontocollege.com If the object is not found in the specified scope. The class specified is instantiated. A new object reference is associated with the variable If the object is not found in the specified scope, and a beanName is specified, then the instantiate() method of the java.bean.Beans is invoked. If the method succeeds, the new object reference is associated with the variable, in the specified scope. What does the container do under the semantics?

41 www.torontocollege.com Jsp:setProperty <jsp:setProperty name=“beanName” propertydetails Propertydetails property = “*” property=“propertyName” property=“propertyName” param=“parametName” property=“propertyName” value=“propertyValue”

42 www.torontocollege.com Jsp:getProperty <jsp:getProperty name=“name” property=“propertyName” Name: The name of the bean instance from which the property is obtained. The bean must already have been found or created using Property: The name of the property to retrieve The jsp:getProperty action is complementary to the jsp:setProperty action and is used to access the properties of a bean. It accesses the value of a property, converts is to a String, and prints it to the output stream.

43 www.torontocollege.com Example of Using JavaBean Components Example : SpellCheck SpellCheck.htm l Wordpro.jsp SpellCheck.class

44 www.torontocollege.com SpellCheck.java /** * This bean encapsulates the functionality to spell check a String */ public class SpellCheck { private String word; public SpellCheck() {} /** Method to reverse the string uses @return the reversed String */

45 www.torontocollege.com public String reverse() { return (new StringBuffer(word).reverse()).toString(); } /** Checks the spelling of the word.This method has no 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.

46 www.torontocollege.com * @return the current value of the word property */ public String getWord() { return word; } /** * Sets the value of the word property. * * @param aWord the new value of the word property */ public void setWord(String aWord) { word = aWord; }

47 www.torontocollege.com Wordpoint.jsp <% System.out.println("Explicitly doing some work on the bean..."); help.setWord(request.getParameter("word")); %>

48 www.torontocollege.com You entered the input(getParameter), You entered the input(jsp:getProperty), The processed output is:

49 www.torontocollege.com spellCheck.html Untitled Document

50 www.torontocollege.com Enter word: Reverse Spellcheck

51 www.torontocollege.com Exercise of Using JavaBean Components In this exercise, you develop a simple JSP page ( form.jsp ), which can process an HTML form containing typical input elements like textboxes, radio buttons, and checkboxes. You also develop a bean ( FormBean.java ), whose property names mirror the input elements of the form. You will then examine the automatic instantiation of the bean on a form POST operation, using the introspective features provided by the JSP engine.

52 www.torontocollege.com Exercise of Using JavaBean Components TASKS You are given the JSP page containing the form. Observe that the form posts to itself recursively. Instantiate the bean FormBean when you recognize that a POST operation has taken place. Allow the setter methods to be called on the bean using introspection. Deploy the JSP page within Tomcat. Develop the bean, FormBean.java, with properties matching the form elements. Compile the bean source FormBean.java. Deploy the bean within Tomcat. Run the example.

53 www.torontocollege.com Jsp:include Example This example shows how the includes work: Including a jsp and html with the include directive...

54 www.torontocollege.com Including a jsp and html with the include action..

55 www.torontocollege.com What is the difference between directive include and jsp:incude The jsp:include is being computed at request time The directive include parses the content of the included files into the current JSP while compiling it. The jsp:include can handle the static and dynamic content. The directive include handles the static content

56 www.torontocollege.com Pass value to another JSP file This example shows how the include work: <jsp:include page=“two.jsp” flush=“true” <jsp:param name=“attribute1” value=“value1” <jsp:param name=“attribute2” value=“value2” In this code, two.jsp can access the values of attribute using request.getParameter(“attribute1”)

57 www.torontocollege.com JSP:include

58 www.torontocollege.com Include Example here is a JSP page that inserts four different snippets into a "What's New?" Web page. Each time the headlines change, authors only need to update the four files, but can leave the main JSP page unchanged.

59 www.torontocollege.com This action lets you forward the request to another page. It has a single attribute, page, which should consist of a relative URL. This could be a static value, or could be computed at request time, as in the two examples below. " /> Jsp:forward

60 www.torontocollege.com The invoking page can also pass the target resource bean parameters by placing them into the request, as shown in the diagram:

61 www.torontocollege.com A tag may also have jsp:param subelements that can provide values for some elements in the request used in the forwarding: " >


Download ppt "Www.torontocollege.com JavaServer Pages Fundamentals."

Similar presentations


Ads by Google