Server-Side Web Programming: Introduction To Java Server Pages
Server-Side Web Programming: Introduction To Java Server Pages
Server-Side Web Programming: Introduction To Java Server Pages
Web Programming
Lecture 3:
Introduction to Java Server Pages
Form Handling
• Form data appended to request string
<FORM NAME="purchaseform"
METHOD=GET
ACTION=http://frodo.cis.ysu.edu/~john/cgi-bin/test.pl>
Quantity: <INPUT TYPE="text" SIZE="8" NAME="quantity" />
<BR /><BR />
<INPUT TYPE="submit" VALUE="SUBMIT">
</FORM>
2
Form Handling
Goal:
– Extract values of parameters from the request
– Generate appropriate response page based on
parameter values
– Take other appropriate action based on request
• Add to shopping cart
• Modify or query database
3
Server Page Model
• Html document with executable code interspersed
• When page requested:
– Code executed
– Html generated and inserted in its place
– Final all html document sent back as response
Tomcat server
request for
somepage.jsp resulting html somepage.jsp
page
html html Java
html html html html Java html
html html html html html html
html html html Java html html
html html html
4
Java Server Pages
<HTML>
<HEAD><TITLE>cgi-bin
response</TITLE></HEAD>
<BODY>
<P>
Thank you for your order of
<%= request.getParameter(“quantity”) %>
widgets!
</P>
</BODY>
</HTML>
5
JSP Syntax
• Basic tag form: <% … %>
• Simplest form:
<%= some Java expression %>
6
JSP Simple Example
• Simple example:
<html>
<body> Java
<p>
Two plus two is <%= 2 + 2 %>. Server
</p> Page
</body>
</html>
<html>
<body> 2 + 2 evaluated to value of 4
<p>
Two plus two is 4.
</p>
</body>
</html> Resulting html Page 7
JSP Syntax
• Basic tag form: <% … %>
• Executes code inside brackets without generating html
– Set variables later used to generate html later
– Store/access values in session/databases
<html>
<body>
<% int sum = 2 + 2; %>
<p>
Two plus two is <%= sum %>.
</p>
</body>
</html>
8
JSP Syntax
<html>
<body> Stores value of 4 in
<% int sum = 2 + 2; %>
<p> sum variable
Two plus two is <%= sum %>.
</p> Value of 4 in sum
</body> used in this JSP
</html>
<html>
<body> No html here
<p>
Two plus two is 4.
</p>
</body>
</html> 9
JSP Scoping
• Variable declared in a
<html>
block of JSP on a page <body>
<% int sum = 2 + 2; %>
• May be accessed by <p>
any other JSP on same
page Two plus two is <%= sum %>.
</p>
</body>
• No access to variable </html>
from any other page
(purpose of sessions)
10
Html Forms
• The FORM tag
<form action=”url of response page”
method=”get or post”>
…
</form>
13
Form Element Example
<form action="orderReply.jsp" method="get">
<table cellspacing="5">
<tr>
<td align="right">Number to purchase:</td>
<td><input type="text" name="quantity"></td>
</tr><tr>
<td align="right">Your name:</td>
<td> <input type="text" name="customerName"></td>
</tr><tr>
<td align="right">Your email:</td>
<td> <input type="text" name="customerEmail"></td>
</tr><tr>
<td></td>
<td><input type="submit" value="Place Order"></td>
</tr>
</table>
<form>
14
Form Element Example
15
Form Parameter Passing
request
Data from form methods to Code in JSP
Other data access data
about request about the request
17
Handling Form Data
• Most useful method:
String request.getParameter(String)
• Example:
String name =
request.getParameter("customerName");
sets the value of “name” to “John Sullins”
18
Example JSP
<body>
<%
String name = request.getParameter("customerName");
String email = request.getParameter("customerEmail");
String quantity = request.getParameter("quantity");
%>
<h2>Order Confirmation</h2>
<p>
Thank you for your order of <%= quantity %> widgets, <%= name %>.
</p>
<p>
You will shortly receive an email confirmation at <%= email %>.
</p>
</body>
19
Acquiring Form Data
• Statements to get and store form data:
<%
String name =
request.getParameter("customerName");
String email =
request.getParameter("customerEmail");
String quantity =
request.getParameter("quantity");
%>
name “John Sullins”
email “[email protected]”
quantity “137”
20
Displaying Values in Response
<p>
Thank you for your order of <%= quantity %>
widgets, <%= name %>.
</p><p> John Sullins 137
You will shortly recieve an email
confirmation at <%= email %>. [email protected]
</p>
21
Commenting JSP Files
• Crucial to future maintenance of site
22
Importing Library Classes
• Much of Java classes in separate libraries
• Must be imported to be used in JSP
• Syntax:
<%@ page import=”list of Java classes” %>
• Example:
<%@ page import=”java.util.Date, Date class
java.io.*” %> in the util
library
All classes in
the io library
23