Download - Introduction to JSP with Forms and JavaBeans
![Page 1: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/1.jpg)
![Page 2: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/2.jpg)
Embeds Java code In HTML tags When used well
Simple way to generate dynamic web-pages When misused (complex embedded Java)
Terribly messy (and may violate OaOO) Keep the embedded Java simple
Use external helper classes (Beans?)
![Page 3: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/3.jpg)
JSP Document
.java file
.class file
.class file ready to run
Response Document
Translation
Compilation
Reinitialization
Subsequent User Requests
![Page 4: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/4.jpg)
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)
![Page 5: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/5.jpg)
<html><head> <title> Hello JSP </title>
</head><body> <p> Hello World: <%= new java.util.Date() %></p></body></html>
![Page 6: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/6.jpg)
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");
![Page 7: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/7.jpg)
![Page 8: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/8.jpg)
So far we’ve seen literals: E.g. <html> Copied straight to output (to browser)
And expressions: E.g. <%= new java.util.Date() %> Return a value included in the output
Also have: Directives, Declarations and Scriptlets
![Page 9: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/9.jpg)
Instructions to the compiler Examples:
Include another page (compile-time) <%@ include file="header.jsp" %> <jsp:include page="page.jsp" flush="true" /> What is the difference ???
Import some Java packages (comma sep.) <%@ page import=“java.util.Collection”%>
![Page 10: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/10.jpg)
These are tags that start with the word jsp as in the previous example
These are mainly for handling form beans as will be shown later apart from 3 tags.
<jsp:forward, to forward user to another page <jsp:forward page="/servlet/login" />
<jsp:plugin, to import plugin as a java applet in the client browser. <jsp:plugin type=applet code="Molecule.class"
codebase="/html"> <jsp:include, to include the result of a jsp
page
![Page 11: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/11.jpg)
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:
<%! int count = 0 %> <%! double sqr(double x) { return x * x; } %>
![Page 12: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/12.jpg)
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 Get the writer: response.getWriter()
They go in the service method of the servlet
Get executed each time a page is requested
![Page 13: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/13.jpg)
Demonstrates much of the above<%! int n = 0; %>
Page accessed: <%= ++n %> times
<% if ( (n % 10) == 0 ) {
n = 0;
}
%>
![Page 14: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/14.jpg)
![Page 15: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/15.jpg)
Application Config Out Request Response Session
![Page 16: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/16.jpg)
Each JSP page has access to two special objects
The Request object carries information passed by the HTTP request (e.g. made by the browser)
This includes any submitted form data The Response object is used to pass
information back to the Client (browser) E.g. response.getWriter() provides an
output stream for direct writing to the client
![Page 17: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/17.jpg)
JSP makes form handling easy Can use request.getParameter() to get
submitted values Or can define a JavaBean to grab the
values semi-automatically. We’ll see this in action later with a simple
example
![Page 18: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/18.jpg)
Global object: request, session
<% session.putValue(“username”, request.getParameter(“UserName”)) %>
<html><body>Thanks for giving us your name, <%= session.getValue(“username”) %>
</body></html>
![Page 19: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/19.jpg)
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>)
![Page 20: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/20.jpg)
Helper classes for servlets To generate forms (+ other HTML) Process the form input
JSP + JavaBeans (more later) JSP + Tag Library (will be covered later)
![Page 21: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/21.jpg)
Come in two types Simple (this course) Enterprise (EJB: more complex, not covered)
Simple JavaBeans Data bound classes Define properties (fields) Define get/set methods
See following example
![Page 22: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/22.jpg)
![Page 23: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/23.jpg)
<body><h3 align="center">Welcome to the Hotel
California</h3><form method="POST" action="BookHotel.jsp"><p>Name: <input type="text" name="name"
size="20"></p><p>How many nights: <select size="1" name="nNights"> <option selected="">1</option> <option>2</option> <option>3</option></select></p><p><input type="submit" value="Submit" name="B1"></p></form></body>
![Page 24: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/24.jpg)
<html><head><title>Bean test</title></head><body><h2> <%=request.getParameter("name") %> to stay for <%= request.getParameter("nNights") %>
nights.</h2> </body></html>
![Page 25: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/25.jpg)
<jsp:useBean id='roomBooking' scope='page' class='beans.HotelBean' />
<jsp:setProperty name='roomBooking' property='*' />
<html><head><title>Bean test</title></head>
<body><h2> <%=roomBooking.getName()%> to stay for <%= roomBooking.getnNights() %> nights. </h2> </body></html>
![Page 26: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/26.jpg)
package beans;public class HotelBean { String name; int nNights;
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getnNights() { return nNights; } public void setnNights(int nNights) { this.nNights = nNights; }}
![Page 27: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/27.jpg)
![Page 28: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/28.jpg)
Note the setting: scope='page' Scope has four possible settings: Page
Bean exists for execution of that page only Request
Like page, but survives any forward or include requests
![Page 29: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/29.jpg)
Session The Bean exists for multiple requests
within the web application, from a particular web browser instance
Used for shopping baskets etc. Application
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)
![Page 30: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/30.jpg)
For this example, consider the differences: JavaBean JSP page: more complex JavaBean also required an external class
definition Discussion question:
When would JavaBean solutions be better than manual versions?
Answer:
![Page 31: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/31.jpg)
<%@ page import="java.util.*" %> <HTML> <BODY> <%!
Date theDate = new Date(); Date getDate() {
return theDate; } %> Hello! The time is now <%= getDate() %> </BODY> </HTML>
![Page 32: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/32.jpg)
The JSP can get really intractable with lots of scriplets
A direct example of using scriplet is a for loop to display records from a database in a table format.
Put the code for the view in a function and call it as much as you need
![Page 33: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/33.jpg)
<%public void printRecord(int Number){
out.print(“<TR>”);out.print(“<TD>Number</TD>”);out.print(“<TD>” +Number+“</TD>”);out.print(“</TR>”);
} %><TABLE BORDER=2><% for ( int i = 0; i < n; i++ ) {
printRecord(i+1); }%></TABLE>
![Page 34: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/34.jpg)
This doesn’t look anymore like an HTML code.
What is the point of JSP then, is it to get messy code!!
So, sun devised a remedy for that and the final result looked really good, but not the development
The solution is called JSP Standard Tag Library or JSTL.
The last example will be written in one tag as
<mylib: for start=“1” end=“10”/>
![Page 35: Introduction to JSP with Forms and JavaBeans](https://reader036.vdocuments.us/reader036/viewer/2022081418/5681459f550346895db29462/html5/thumbnails/35.jpg)
Next Lecture