Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.

Similar presentations


Presentation on theme: "1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is."— Presentation transcript:

1 1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is first requested, a JSP is parsed into a java source file which is then compiled into a servlet class and run under the container JVM Initially, there is a performance hit However, there is a “build” mechanism to avoid unneeded parsing and compilation based on the date of the JSP page relative to the class file

2 2 Java Server Page Tags Directives Hidden Comments Declarations Expressions Scriptlets There are also a number of XML-type tags that begin with “<jsp:’ called “actions”

3 3 Directives Directives are compiler instructions that are processed when the page is compiled The format is Examples:

4 4 Directives At the top of each JSP is a page declaration <%@ page language=“java” language (see below) import=“...” list of packages to import errorPage=“...”substitute error page contentType=“...”usually “text/html” %> At present only Java can be used in a JSP

5 5 Directives This directive inserts the “static” contents of a file into the JSP at compile time The text becomes part of the JSP page It is useful for including code that is common to a number of pages, but for which you do not want to use a bean Example:

6 6 Hidden Comments These are used to document the page and are not sent to the client The format is Example: Any characters may be used in the body of a comment except the closing "--%>" marker which should be escaped as "--%\>".

7 7 Declarations These are used to declare variables or methods that have scope throughout the page The format is Examples: <%! String myString = "hello"; private String getData() { // Java code for the method goes here } %>

8 8 Declarations Every JSP contains implicit references to API objects that do not need to be declared: –request –response –pageContext –session –application –out –config –page –exception

9 9 Expressions The result of an expression is converted to a String and inserted into the output stream at the place where it occurs The format is Examples: Note that the expression is not terminated with a semicolon The parser simply wraps an out.print() or similar method around the expression as its argument

10 10 Scriptlets Scriptlets contain fragments of Java code The format is Scriptlet code can access implicit references and declared variables or methods Use a scriptlet to wrap a conditional or a loop around a block of HTML Examples: Please sign up! Welcome back!

11 11 Scriptlets The output HTML stream will vary depending on the value of the request parameter named “user” Note the use of the implicit request object which does not need to be declared with “ ” Example of scriptlets with an expression: The current number is:

12 12 Action This tag is used to include either static or dynamic content in the output stream If the file is dynamic, you can use to pass parameters to it Examples:

13 13 Action This tag takes the implicit request object (which contains the client request parameters and other CGI-type data) and forwards it to a target file - an HTML page, a servlet, or another JSP file Example: "> The tag is used to pass additional parameters to the target

14 14 Action: Java Beans JavaBeans are the component model for Java - the equivalent of all those fancy components you can obtain and use in the.NET world In a component model / market, developers go to the marketplace, pay for the component library they need, and wire it all together in a JSP using their IDE This has worked for other languages (e.g. Visual Basic), but has never developed for the Java market

15 15 Action: Java Beans A Java Bean is a class with the following –A default constructor (with no argument list) –A Set and Get method for each attribute (called a property in Java Bean terms) A JSP can use an action to instantiate and access an instance of the Java Bean class A JSP can use actions to access the other methods of an instance of the Java Bean A JavaBean can be monitored by listeners for changes to its parameters from the JSP

16 Java Bean public class MyJavaBean { private String propertyName; public MyJavaBean() { … } // Default Constructor public void setPropertyName(String theValue) { this.propertyName = theValue; } public String getPropertyName() { return this.propertyName; } } 16

17 17 Action: Java Beans and Example of how to set up a bean instance

18 18 Action: Java Beans This tag retrieves the value of a bean property and converts it to a String to display it in the page The format is: The name attribute points to the bean with the same id attribute used in the tag The property attribute is the name of the bean property you wish to display (there must be a corresponding Get method in the bean)

19 19 Java Beans in Scriptlets / Expressions Once a Java Bean has been instantiated, its instance name and its methods can be used in scriptlets: <% beanInstanceName.setPropertyName (“New_value”) %> or expressions:

20 20 Java Server Pages You can view the Java source code generated by the parser, but it's not a good idea to modify it The generated Java code is found in: Tomcat 6.0\work\Catalina\localhost\myapp\org\apache\jsp The class file for any Java Beans used must be in this directory for it to be found while compiling the generated JSP source code A copy of the class file for any Java Beans must also be located in the WEB-INF/classes directory for Tomcat to find it at run time (Proper classpath setting would probably fix this)

21 Model-View-Controller Multiple views of enterprise application and information is required – one for each type of user Leads to MVC Architecture – separation of functions Java Server Pages, Servlets, and Java Beans can be used to implement the MVC architecture for access to common application and/or information

22 Model-View-Controller The MVC Solution Enterprise Information System HTML View Classic Web Client Administrator Swing View XML Web Service Supplier B2B Agent

23 Model-View-Controller View (HTML/JSP) -Renders the Model -Requests updates from model(s) -Sends user gestures to controller -Allows controller to select view Controller (Servlets) -Defines application behavior -Maps user actions to model updates -Selects views for response -One for each functionality Model (Java Beans) -Encapsulates application state -Responds to state queries -Exposes application functionality -Notifies view(s) of changes Change Notification State Query State Change View Selection User gestures


Download ppt "1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is."

Similar presentations


Ads by Google