More on Processing User Input BCIS 3680 Enterprise Programming

advertisement
More on Processing User Input
BCIS 3680 Enterprise Programming
Overview
More input fields
More JSP elements





Scriptlet
Declaration
Directive
Useful techniques




2
Session variables
Redirecting traffic
Comparing strings
Processing User Input
Again, each piece of user input is passed as a parameter.
Just like with servlets, use getParameter() method to retrieve
the value for a parameter.
Be very careful with parameters that may be passed as null
(e.g., checkboxes), or textboxes that can’t be made mandatory
according to business logic).
If user enters a value, it is passed as a String (with exact
capitalization as entered by the user). If user skips a value (and
your business logic allows it), it is passed as a null object.




A checkbox is an optional item. So if it is not checked and you
try to get it as a parameter, you will get a null object.

3
Dropdown List
Use the <select> element to create a dropdown list.
The “name” attribute is its unique identifier that
distinguishes it from other controls.
Each selectable item is defined in an <option> element,
which is a “child” element of <select>.
The item that is selected by default is marked by the
“selected” attribute.
The corresponding parameter has:







4
A name that comes from the name attribute of the
<select> element;
A value that comes from the value attribute of the
<option> element selected by the user.
Dropdown List
<p>State:
<select name="state">
<option value="TX" selected>
Texas</option>
<option value="FL">Florida</option>
<option value="CA">California</option>
</select></p>
5
Hidden Input
Hidden input also is created with the <input> element.
Set the “type” attribute to “hidden”.
It does not appear anywhere in page display, but the
<input> element is visible in the HTML source of the
page.



Use it only to pass information that would become a
distraction or cause confusion for the user if it is displayed.
 Don’t use it to hide any data that may cause privacy concern.
<input type="hidden" name="agent" value=
"bond" /input>
 This will pass the name-value pair “agent=007” to the
“action” page.

6
Textarea
The <input> element of “text” type creates a singleline input box for textual information.
To have multi-line text boxes, use the <textarea>
element instead.
Define the number of rows and columns you want.



<p>Comments:<br />
<textarea name="comments" rows="5"
cols="30">
</textarea></p>
7
Review: JSP Elements
Element
Opening &
Closing Tags
Description
Expression
<%=
%>
Segment of Java statement that resolves to
a String value.
Scriptlet
<%
%>
One or more complete Java statements
grouped together logically.
Declaration
<%!
%>
Equivalent to a method in a Java class.
Directives
<%@
%>
Special instructions; e.g.,The “import”
attribute of the “page” directive is
equivalent to the import statement in
Java.
8
Declaration
To make your code more modular like that in a Java class,
you can use the declaration to define methods inside a
JSP file.
Now not only can your JSP expressions refer to variable
names, but also make method calls.



9
This provides more flexibility in how you mix JSP expressions
and HTML code.
Directive



<%@ directive attribute1="value1",
attribute2="value2", … %>
For now we’re only interested in the page and
include directives.
The page directive has an import attribute that is
equivalent to the import statement in Java.


The import=value pair is only one attribute. Therefore, to
import more than one class, all classes must be included within
one pair of quotation marks, separated by commas.
The include directive can be used to attach the same
header and footer to a number of pages.
10
import Attribute of page Directive




Make Java classes available to the scripting environment.
Use Java API to add power to your JSP script.
When a Java class is imported to a JSP page, you can
reference this class without explicitly specify the class
package names.
Syntax
<%@ page import="class1[,class2,…classN]" %>
<%@ page import="java.util.Date,java.sql.*" %>

Similar to the import statement in Java.
11
Location of User-Defined Classes


To use those classes that ship with the Java language, the
import attribute of the page directive is sufficient.
To use custom made classes, additional work is needed:


12
First, compile the class to be used from its source code into a
.class file.
Copy the compiled file to the proper subfolder in the
application’s deployment folder (see next slide).
Proper Placement of Class Files


Review
http://www.cob.unt.edu/itds/faculty/wu/bcis3680/docs/16sp3680-03-packages.pptx
Do the following in the application’s deployment folder




13
Create a “WEB-INF” subfolder if it doesn’t already exist.
Inside WEB-INF, create a “classes” subfolder.
Inside classes subfolder, create a subfolder whose name is the
same as the name of the package to which the class belongs. For
example, “unt” subfolder if the class belongs to the “unt” package.
If the package name is hierarchical, the subfolder structure should
mirror that hierarchy. For example, store the class files for the
wu.andy package in <deployment folder>\WEBINF\classes\wu\andy.
Include Files

Usage


14
Provide the same navigation links and footer on each of the
pages.
Reuse JSP code.
The include Directive
<%@ include file="relative URL of included file" %>



When the JSP page containing the include directive is
requested for the first time, the content of the included
file is read and merged with the rest of the JSP page.
The order matters. For example, if the include
directive is placed after the <title> tag of the JSP page,
the words enclosed by that tag is displayed in the title bar.
If the include directive appears before the <title>
tag, then the included page’s title takes precedence.
After the JSP page has been displayed, updating the
included file has no impact on the JSP page.
15
Comparing Strings


When used with two strings, the equality operator
compares the memory locations of the strings.
The comparison methods compare text. They do not
compare the memory locations marked by the two references
to the strings.



The addresses are certainly different, unless you assign one to the
other.
That’s why we cannot compare the texts stored in two strings by
something like: if (str1 == str2). This boolean expression
compares the memory locations of str1 and str2, not the texts.
Normally, we simply want to compare the literal values
contained in two strings (and by extension, in two memory
locations), but not the memory locations themselves.
16
Comparing Strings
Comparison
What Are Compared
Using equals() or
equalsIgnoreCase()
The texts (literal values)
stored in the two memory
locations allocated to the two
strings.
Very useful in
business
applications
Using the equality
operator (==)
The memory locations of the
two strings (always different
even though they may
contain the exact same text).
Rarely useful in
business
applications
17
Practical Use
Comparing Strings


A common task in text processing is to compare two
strings and see whether they are identical.
If the comparison is case sensitive (namely, “BCIS” is
considered different from “bcis”):


Use the equals() method of the String class.
If the comparison is case insensitive (namely, “BCIS” is
considered the same as “bcis”):


18
Option 1: Use the equalsIgnoreCase() method.
Option 2: Convert both strings into the same case (using
either toUpperCase() or toLowerCase() method) and
then use the equal() method to compare them.
Methods Involved in Comparison
Return type
Signature
boolean
equals( String str )
Evaluates to true if the calling string is the same as
str, case-sensitive; returns false otherwise.
boolean
equalsIgnoreCase( String str )
Evaluates to true if the calling string is the same as
str, case-insensitive; returns false otherwise.
String
toUpperCase()
String
Converts all the letters in the calling string to upper case.
toLowerCase()
Converts all the letters in the calling string to lower case.
19
Redirecting Traffic




Sometimes, you may want to display different pages to the
user, depending on his or her input from the previous
page.
Retrieve the value of the criterion from the related
parameter.
Evaluate the parameter and determine which URL to use.
If another page instead of the current page should be
displayed, call the sendRedirect() method of the
response object, using that page’s file name or URL as
the argument.
response.sendRedirect(<destination>);

Do this before any other JSP code in the page is run.
20
Download