Presentation on theme: "More on Processing User Input BCIS 3680 Enterprise Programming."— Presentation transcript:
More on Processing User Input BCIS 3680 Enterprise Programming
Overview 2 More input fields More JSP elements Scriptlet Declaration Directive Useful techniques 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 4 Use the 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 element, which is a “child” element of. The item that is selected by default is marked by the “ selected ” attribute. The corresponding parameter has: A name that comes from the name attribute of the element; A value that comes from the value attribute of the element selected by the user.
Dropdown List 5 State: Texas Florida California
Hidden Input 6 Hidden input also is created with the element. Set the “ type ” attribute to “ hidden ”. It does not appear anywhere in page display, but the 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. This will pass the name-value pair “ agent=007 ” to the “ action ” page.
Textarea 7 The element of “ text ” type creates a single- line input box for textual information. To have multi-line text boxes, use the element instead. Define the number of rows and columns you want. Comments:
Review: JSP Elements ElementOpening & 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. This provides more flexibility in how you mix JSP expressions and HTML code. 9
Directive 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
The import Attribute 11 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 Similar to the import statement in Java.
Location of User-Defined Classes 12 To use those classes that ship with the Java language, the import attribute of the pag e directive is sufficient. To use custom made classes, additional work is needed: First, compile the class from source code into a.class file. In the deployment folder, create a “ WEB-INF ” subfolder. 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 \WEB- INF\classes\wu\andy.
Include Files 13 Usage Provide the same navigation links and footer on each of the pages. Reuse JSP code.
14 The include Directive 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 tag of the JSP page, the words enclosed by that tag is displayed in the title bar. If the include directive appears before the 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.
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. 15
Comparing Strings 16 ComparisonWhat Are ComparedPractical Use 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
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”): 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. 17
Methods Involved in Comparison 18 Return typeSignature booleanequals( String str ) Evaluates to true if the calling string is the same as str, case-sensitive; returns false otherwise. booleanequalsIgnoreCase( String str ) Evaluates to true if the calling string is the same as str, case-insensitive; returns false otherwise. StringtoUpperCase() Converts all the letters in the calling string to upper case. StringtoLowerCase() Converts all the letters in the calling string to lower case.
Redirecting Traffic 19 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( ); Do this before any other JSP code in the page is run.