Академический Документы
Профессиональный Документы
Культура Документы
Overview
Embeds Java code In HTML tags When used well
Simple way to generate dynamic web-pages Terribly messy (and may violate OaOO) Use external helper classes (Beans?)
Life Cycle
A JSP page is translated into a Java Servlet And then compiled On Tomcat, the compilation happens the first time a page is requested First request can be very slow! Afterwards, just as fast as a Servlet (because it is then a servlet)
Hello World
<html> <head> <title> Hello JSP </title> </head> <body> <p> Hello World: <%= new java.util.Date() %> </p> </body> </html> See also: Date_jsp.java the Servlet this page is translated to
Date_jsp.java (extract)
This extract shows the part that produces the output compare it with the JSP: out = pageContext.getOut(); _jspx_out = out;
out.write("<html>\r\n"); out.write("<head> "); out.write("<title> Hello JSP "); out.write("</title> "); out.write("</head>\r\n"); out.write("<body> \r\n"); out.write("<p> Hello World:\r\n "); out.print( new java.util.Date() ); out.write("\r\n"); out.write("</p>\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n");
Produced
Basic Constructs
So far weve seen literals:
E.g. <html> Copied straight to output (to browser) E.g. <%= new java.util.Date() %> Return a value included in the output Directives, Declarations and Scriptlets
And expressions:
Also have:
Directives
Instructions to the compiler Examples:
Declarations
Used to declare variables and methods Go in the declaration section of the Servlet Can then be used later in the JSP page Note the syntax Examples:
Scriptlets
These are sections of Java code embedded in the page Unlike expressions, they do not return a value But may write directly to the page
They go in the service method of the servlet Get executed each time a page is requested
Illustrative Example
Demonstrates much of the above <%! int n = 0; %> Page accessed: <%= ++n %> times <% if ( (n % 10) == 0 ) { n = 0; } %>
HTML Forms
Allow user to supply input
Text Fields single line Password field single line, blanked-out text Text Areas multi-line Choice (pop-up menu) Radio-button (1 from n) Check-box (m from n) Browse button Submit buttons
Form Generation
Need to generate the HTML to send to the client The forms should be well presented
Need to name the input fields So that we may extract the data from the submitted form Next: some sample form elements
Form Processing
The form can specify whether data is supplied via a GET or POST request POST is the usual way Therefore, a servlet should implement the doPost() method to process a form JSP hides these GET/POST details (see request.getParameter and <jsp:setProperty>)
JSP + JavaBeans (more later) JSP + Tag Library (not covered in this course) XForms not covered, but well worth a read if youre keen! MS InfoPath installed in the Lab can auto-generate forms from XML Schemas
JavaBeans
Come in two types
Simple (this course) Enterprise (EJB: more complex, not covered) Data bound classes Define properties (fields) Define get/set methods
Simple JavaBeans
Resulting Page
Bean Scope
Note the setting: scope='page' Scope has four possible settings: Page
Bean exists for execution of that page only Like page, but survives any forward or include requests
Request
The Bean exists for multiple requests within the web application, from a particular web browser instance Used for shopping baskets etc. The Bean exists for all requests from all users, for all pages that use it. Survives until the Web Application Server is restarted Can be used for a database connection (or connection pool)
Application
Beans v. Manual
For this example, consider the differences:
JavaBean JSP page: more complex JavaBean also required an external class definition When would JavaBean solutions be better than manual versions?
Discussion question:
Answer:
Lab Exercises
Try typing in and running running these JSP examples for yourself Instructions for Tomcat: see Lab pages Also study the servlet code produced (in the tomcat\work directory