Server-Side Web Programming: Java Server Pages For Complex Forms
Server-Side Web Programming: Java Server Pages For Complex Forms
Web Programming
Lecture 4:
Java Server Pages for
Complex Forms
Parsing Numeric Input
• getParameter method returns a String
2
Parsing Numeric Input
Useful built-in methods:
• Parsing a whole number string (such as “57”):
int Integer.parseInt(String)
• Parsing a decimal string (such as “5.7”)
double Double.parseDouble(String)
• Example:
String quantity = request.getParameter(“quantity”);
int quantityNumber = Integer.parseInt(quantity);
3
Numeric Input Example
<%
String name = request.getParameter("customerName");
String email = request.getParameter("customerEmail");
String quantity = request.getParameter("quantity");
double pricePerUnit = 9.95;
int quantityNumber = Integer.parseInt(quantity);
double totalCost = pricePerUnit * quantityNumber;
%>
<h2>Order Confirmation</h2>
<p>Thank you for your order of <%= quantity %> widgets, <%= name
%>.</p>
<p>At $<%= pricePerUnit %>, your bill will be $<%= totalCost %>.</p>
<p>You will shortly recieve an email confirmation at <%= email %>.</p>
4
Numeric Input Example
5
Complex Input Elements
• Checkboxes
• Radio Buttons
• Lists
• Require more
complex handling
– Will do truly complex
handling in multipage
servlet structures
6
Example Result
7
Checkbox HTML
• Basic form:
<INPUT TYPE="checkbox“ NAME="monitor">
Monitor
8
Checkbox JSP
• If execute JSP code:
String monitor =
request.getParameter("monitor");
9
Conditions in Java
• JSP may need to do different things depending
on checkbox
– Display “Monitor” if checked
– Display nothing if not checked
• This requires a Java condition
– Basic syntax like C++/JavaScript
if(condition) {
statements to execute if true
}
else {
statements to execute if false
} 10
Conditional HTML Display
• Key: Display different html based on condition
• Put html in conditional statement
– Must use <% and %> to differentiate Java, html
<%
if (condition) {
%>
html to display if condition true
<%
}
else {
%>
html to display if condition false
<%
}
%> 11
Checkbox Example
<%
if (monitor != null) {
%>
If this Java condition
Monitor<br> is true (the monitor is
not null)
<%
}
Display this html
%>
12
Radio Button HTML
• Convention: Only one in group checked
• Must give all in group same name
• Must give each different VALUE
<INPUT TYPE="radio"
NAME="processor" VALUE="Celeron D">
Celeron D<BR>
<INPUT TYPE="radio"
NAME="processor" VALUE="Pentium IV">
Pentium IV<BR>
<INPUT TYPE="radio"
NAME="processor" VALUE="Pentium D">
13
Pentium D
Radio Button JSP
• Sent in form …name= value… to server
– processor=Celeron+D
– processor=Pentium+IV
– processor=Pentium+D
• Can access value using:
String processor =
request.getParameter("processor");
And display in html:
<%= processor %>
14
String Comparison
• May need to base html on value passed:
16
Detecting Null Input
Basic form of code:
if (variable != null) {
if (variable.equals(value1)) {…}
if (variable.equals(value2)) {…}
}
else {
code for case where no button selected
}
17
Detecting Null Input
Example:
<% if (processor != null) { %>
<%= processor %>
<% if (processor.equals("Celeron D")) { %>
<br/><i>Have you considered a more
powerful processor?</i>
<% } %>
<% }
else {
%>
No processor selected.
<%
}
%>
18
Detecting Null Input
21
Multiple Selection Lists
• Can allow user to select multiple options from list
with MULTIPLE attribute
<SELECT NAME="peripherals" SIZE="3“
MULTIPLE>
23
Looping Through Arrays
• Often use loop to process all values selected
• Basic form in Java:
for (int i = 0; i < arrayname.length; i++) {
code to process ith element of arrayname
}
Note: Java has built-in length property
for array which evaluates to its size
peripherals[0]
peripherals[1]
25
Checking for NULL Lists
• User may not choose any value in a list
• request.parameterValues will return null instead of
an array
• Must check for this before processing array
if (arrayname != null) {
for (int i = 0; i < arrayname.length; i++) {…}
}
else {
code to handle case where no option selected
}
26
Checking for NULL Lists
• Example:
27