0% found this document useful (0 votes)
15 views27 pages

Server-Side Web Programming: Java Server Pages For Complex Forms

This document discusses parsing numeric input from HTML forms in Java Server Pages (JSP). It explains that getParameter returns strings that must be parsed to numbers using methods like Integer.parseInt() and Double.parseDouble(). Examples are given for handling checkboxes, radio buttons, and lists to display different HTML or process selections conditionally based on the input. Loops are used to process multiple selections from lists.

Uploaded by

Hop Huynh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
15 views27 pages

Server-Side Web Programming: Java Server Pages For Complex Forms

This document discusses parsing numeric input from HTML forms in Java Server Pages (JSP). It explains that getParameter returns strings that must be parsed to numbers using methods like Integer.parseInt() and Double.parseDouble(). Examples are given for handling checkboxes, radio buttons, and lists to display different HTML or process selections conditionally based on the input. Loops are used to process multiple selections from lists.

Uploaded by

Hop Huynh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 27

Server-side

Web Programming
Lecture 4:
Java Server Pages for
Complex Forms
Parsing Numeric Input
• getParameter method returns a String

This is “5”, not the number 5!

• Must parse strings into numbers before


performing numeric computations

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

• String passed to server:


– “ …monitor=on… ” if monitor is checked
– No mention of monitor if not checked

8
Checkbox JSP
• If execute JSP code:
String monitor =
request.getParameter("monitor");

monitor will have the value


– “on” if the checkbox was checked
– null if the checkbox not checked
• null is always returned if ask for value of parameter which
was not passed in the request string

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:

• Must use .equals method to compare strings


Basic form:
if (string1.equals(string2) { …
• Example:
<% if (processor.equals(“Celeron D”) { %>
<br/><i>Have you considered a more powerful
processor?</i>
15
<% } %>
Null Input
• User may not choose any radio button!
– processor will have value null
– Executing .equals on null will give run time exception
• User should never see this!

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

• Note: At this level of complexity, may handle


with separate redirection servlet
19
List HTML
• Basic form:
<SELECT NAME=“listname” SIZE=“numvisible”>
<OPTION VALUE=“value1”/> label1
<OPTION VALUE=“value2”/> label2

</SELECT>
• Example:
<SELECT NAME="peripherals" SIZE="3">
<OPTION VALUE="Camera“/>Camera
<OPTION VALUE="Printer“/>Printer
<OPTION VALUE="Scanner“/>Scanner
</SELECT> 20
List JSP
• Sent in form …name= value… to server
– peripherals=Camera
– peripherals=Printer
– peripherals=Scanner
• Can access value using:
String peripherals =
request.getParameter("peripherals");

21
Multiple Selection Lists
• Can allow user to select multiple options from list
with MULTIPLE attribute
<SELECT NAME="peripherals" SIZE="3“

MULTIPLE>

• Sends name=value string for each option selected


…peripherals=camera&peripherals=scanner…
• getParameter method will not work!
22
JSP for Multiple Selection Lists
• String[] request.getParameterValues(String
name)
Returns array of values passed for this name
• Example:
String [] peripherals =
request.getParameterValues("peripherals");
creates the array:
peripherals
0 “Camera”
1 “Scanner”

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

• Can use to display html multiple times


<% for (int i = 0; i < arrayname.length; index++) { %>
html created from ith element of arrayname
<% } %>
24
Looping Through Arrays in JSP
• Example:
<% for (int i = 0; i < peripherals.length; i++) { %>
<%= peripherals[i] %><br\>
<% } %>
For each value in the
peripherals array

Display that value


in html

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:

<% if (peripherals != null) { %>


<% for (int i = 0; i < peripherals.length; i++) { %>
<%= peripherals[i] %><br>
<% } %>
<% } %> Only executed if peripherals exist

27

You might also like