1 / 23

Java Server Pages

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

Download Presentation

Java Server Pages

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. 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. 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. Directives • Directives are compiler instructions that are processed when the page is compiled • The format is <%@ and end with %> • Examples: <%@ page %> <%@ include %>

  4. Directives • <%@ page %> • 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. Directives • <%@ include %> • 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:<%@ include file="relativeURL" %>

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

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

  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. 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 <%= expression %> • Examples: <%= myString %> <%= getData() %> • 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. Scriptlets • Scriptlets contain fragments of Java code • The format is <% Java code here %> • 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: <% if(request.getParameter("user").equals("new")) { %> <B>Please sign up!</B> <% } else { %> <B>Welcome back!</B> <% } %>

  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: • <% for(int i=1; i<11; i++) { %>The current number is: <%= i %><BR><% } %>

  12. Action • <jsp:include> • This tag is used to include either static or dynamic content in the output stream • If the file is dynamic, you can use <jsp:param> to pass parameters to it • Examples: <jsp:include page="/login.jsp"> <jsp:param name="username" value="Fred"/> </jsp:include>

  13. Action • <jsp:forward> • 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: <jsp:forward page="relativeURL|<%= expression %>"> <jsp:param name=". . ." value=". . ."/> </jsp:forward> • The <jsp:param> tag is used to pass additional parameters to the target

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

  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; } }

  17. Action: Java Beans • <jsp:useBean> and <jsp:setProperty> • Example of how to set up a bean instance <jsp:useBean id="beanInstanceName"scope="page|request|session|application"class=“MyJavaBean"><jsp:setProperty name="beanInstanceName"property="propertyName" value="theValue" /></jsp:useBean>

  18. Action: Java Beans • <jsp:getProperty> • This tag retrieves the value of a bean property and converts it to a String to display it in the page • The format is:<jsp:getProperty name="beanInstanceName“ property="propertyName"/> • The name attribute points to the bean with the same id attribute used in the <jsp:useBean> 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. 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: <%= beanInstanceName.toString() %> 19

  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 Classic Web Client Supplier B2B Agent Administrator HTML View Swing View XML Web Service Enterprise Information System

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

More Related