Introduction To JSP With Forms and Javabeans
Introduction To JSP With Forms and Javabeans
Translation
.java file
Compilation
.class file
Reinitialization
Subsequent
User Requests
Response Document
Java Server Pages (JSP)
Fundamentals
• Example
• <%@ include file=“included.jsp” %>
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>
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 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
Directives
• 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”%>
JSP Tags
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
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:
• <%! int count = 0 %>
• <%! double sqr(double x) {
return x * x; } %>
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
• Get the writer: response.getWriter()
• 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;
}
%>
What happens on next
refresh?
JSP Actions
Basics & Types
• Processed during the request processing phase.
• As opposed to JSP directives which are processed during translation
• Standard actions should be supported by J2EE compliant web servers
• Custom actions can be created using tag libraries
• The different actions are
• Include action
• Forward action
• Param action
• useBean action
• getProperty action
• setProperty action
• plugIn action
JSP Actions
Include
• Example
• <jsp:include page=“inlcudedPage.jsp”>
• Includes the the output of includedPage.jsp into the page where this is
included.
JSP Actions
Forward
• Example
• <jsp:forward page=“Forwarded.html”>
• Forwards the request to Forwarded.html
JSP objects
• Application
• Config
• Out
• Request
• Response
• Session
Request and Response
• 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
Form Handling with JSP
• 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
Processing form input
Global object: request, session
<% session.putValue(“username”,
request.getParameter(“UserName”)) %>
<html><body>
Thanks for giving us your name, <%=
session.getValue(“username”) %>
</body></html>
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>)
Form Processing
Architectures
• Helper classes for servlets
• To generate forms (+ other HTML)
• Process the form input
• JSP + JavaBeans (more later)
• JSP + Tag Library (will be covered later)
JavaBeans
• 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
Hotel Booking Form
HTML (body) for Booking Form
<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>
Accessing Submitted Values
(manual version)
<html>
<head><title>Bean test</title></head>
<body>
<h2>
<%=request.getParameter("name") %>
to stay for
<%= request.getParameter("nNights") %>
nights.
</h2>
</body>
</html>
JavaBean Version (Auto)
<jsp:useBean id='roomBooking'
scope='page'
class='beans.HotelBean'
/>
<html>
<head><title>Bean test</title></head>
<body>
<h2> <%=roomBooking.getName()%>
to stay for
<%= roomBooking.getnNights() %> nights. </h2>
</body>
</html>
Java Code for Bean Version
package beans;
public class HotelBean {
String name;
int nNights;
getPropertyName()
For example, if property name is firstName, your method name would
be getFirstName() to read that property.
1 This method is called accessor.
setPropertyName()
For example, if property name is firstName, your method name would
be setFirstName() to write that property.
2 This method is called mutator.
Accessing JavaBeans