This article describes the client-side validation, common output, and JSTL basic usage of the JSP introductory tutorial. Share to everyone for your reference. Specifically as follows:
First, the goal:
① Master the basic process of client verification;
② the way to master JSP output information;
③ Master the basic usage of JSTL.
Second, the main content:
① introduces the basic process of client authentication through an example;
② introduces the basic way of JSP output information;
③ introduces the basic usage of JSTL through the example analysis.
Client-side validation-related code is ubiquitous on the web and is very common, so you don't usually need to write it yourself, but you need to know how to use it and how to modify it. The basic usage procedures are described below:
1. How to embed JavaScript code
Using Javasript code to complete client-side validation of user input information, the process of embedding JavaScript code in a page is as follows:
<script language= "JavaScript" >
//Embedding JavaScript code here
</script>
The JavaScript code must be between this start and end flags.
2. How to write JavaScript method
The various validation processes are in the form of methods, and JavaScript methods are defined in the following ways:
function method Name (argument list)
{
//method body
}
Unlike method definitions in Java, function declarations define methods, do not need to return value types, can return arbitrary results, and argument lists do not need to give the type of the argument. The following is an example of a method:
function validate (form)
{
...
}
The following is a method to determine whether a parameter is a number:
Determine if the number
function Isnumber (str)
{
for (i=0;i<str.length;i++)
{
//If you want to determine decimals, you need to determine the decimal point
if (Str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 '
| | str.charat (i) = = "-" && i==0)
continue ;
else return
false;
return true;
}
3, how to establish the relationship between form submission and verification methods?
Complete with the OnSubmit event for form form.
<form name= "Form1" method= "Post" action= "process.jsp" onsubmit= "return
isvalidate (Form1)" >
Where: onsubmit= the "return Isvalidate (Form1)" section is a call to the validation method.
Note: The Submit event for form is used at this time, and the Submit button is used.
You can also modify the submit button to a normal button, and then use the button's OnClick event to invoke the validation method.
4, in the verification of the time to get input information, if obtained?
The form's name until the form element, and then the value is obtained. For example:
Copy Code code as follows:
UserID = Form.userid.value;
Variables do not need to be defined to be used directly.
5. Example: Verify the username and password in the registration function
<%@ page contenttype= "text/html;charset=gb2312"%> <script language= "JavaScript" >//Validation method function Isval
Idate (form) {UserID = Form.userid.value;
if (userid== "") {alert ("User ID cannot be blank");
return false;
}else if (userid.length>8 | | userid.length<6) {alert ("length should be 6-8 bits");
return false;
} Userpass=form.userpass.value; if (userpass.length!=8) {alert ("Password length is not 8!")
");
return false;
return true; </script> please register <br> <form name= "Form1" method= "Post" action= "process.jsp" onsubmit= "Return Isvalidate ( Form1) "> User id:<input type=" text name= "userid" > User ID length is 6-8 bits <br> password: <input type= "password" name= " Userpass "> Requires password length for 8<br> confirmation password: <input type=" password "name=" Userpass1 "><br> Sex: <input type=" Radio "name=" Sex "value=" male "checked> male <input type=" Radio "name=" Sex "value=" female "> <br> hobby: <input type= "checkbox" Name= "fav" value= "movement" > Movement <input type= "checkbox" Name= "fav" value= "Music" > SoundLe <input type= "checkbox" Name= "fav" value= "Programming" > Programming <br> Education: <select name= "Degree" > <option val
Ue= "Undergraduate" > Undergraduate </option> <option value= "master" > Master </option> <option value= "Specialist" > Specialist </option> <option value= "Doctor" > Doctor </option> </select><br> Note: <textarea name= "comment" ></texta
Rea><br> <input type= "Submit" value= "submitted" ><input type= "reset" value= "reset" > </form>
6, commonly used verification: through the regiest.jsp file introduction
This validation is not the most comprehensive and not optimal, if you need to be able to search through the network, you can also check the JavaScript related books, there are many ready-made JavaScript methods available. In addition, regular expressions can be used when client-side validation is done, and is more convenient.
The following code is for reference:
<%@ page contenttype= "text/html;charset=gb2312"%> <script language= "JavaScript" > Function isvalidate (
form) {//Get user input information UserID = Form.userid.value;
Username = Form.username.value;
Userpass = Form.userpass.value;
Userpass2 = Form.userpass2.value;
Birthday = Form.birthday.value;
email = form.email.value;
address = Form.address.value;
Phone = Form.phone.value; Determines the user ID length if (!minlength (userid,6)) {alert ("User ID length is less than 6 bits!")
");
Form.userid.focus ();
return false; } if (!maxlength (userid,8)) {alert ("User ID length is greater than 8 bits!")
");
Form.userid.focus ();
return false; //Judge username length if (!minlength (username,6)) {alert ("User name length is less than 6 bits!")
");
Form.username.focus ();
return false; } if (!maxlength (username,8)) {alert ("User name is longer than 8 bits!")
");
Form.username.focus ();
return false; //Judge Password length if (!minlength (userpass,6)) {alert ("password length is less than 6 bits!")
");
Form.userpass.focus ();
return false; } if (!maxlength (userpass,8)) {alert ("Password length is greater than 8 bits!") ");
Form.userpass.focus ();
return false; //Determine if user name and password are the same if (Username==userpass) {alert ("username and password cannot be equal!")
");
Form.userpass.focus ();
return false; //Verify two times password is the same if (Userpass!= userpass2) {alert ("Two passwords entered differently!")
");
Form.userpass.focus ();
return false; //Verify that the birthday is in the correct format if (!isdate (birthday)) {alert ("Birthday is not in the correct format!")
");
Form.birthday.focus ();
return false; //Verify that the email format is correct if (!isemail (email)) {alert ("email format is incorrect!)
");
Form.email.focus ();
return false;
//Verify that the phone number is in the correct format if (!isdigital (phone)) {alert ("Incorrect format of phone number");
Form.phone.focus ();
return false; //Verify the length of the address is correct if (!maxlength (address,50)) {alert ("address length is greater than 50 bits!")
");
Form.address.focus ();
return false;
return true;
//Verify that NULL function IsNull (str) {if (str.length==0) return true;
else return false;
//Verify that the minimum length function minlength (str,length) {if (str.length>=length) return true is met;
else return false; }//Judge whethersatisfies the maximum length function maxLength (str,length) {if (str.length<=length) return true;
else return false; ///Determine if numeric function isdigital (str) {for (i=0;i<str.length;i++) {//Allow hyphen if (Str.charat (i) >= ' 0 ' &am p;& Str.charat (i) <= ' 9 ' | |
Str.charat (i) = = "-" && i!=0 && i!=str.length-1) continue;
else return false;
return true; //To determine if it is an integer function Isnumber (str) {for (i=0;i<str.length;i++) {//If you want to determine decimals, you need to determine the decimal point if (Str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 ' | |
Str.charat (i) = = "-" && i==0) continue;
else return false;
return true;
//Determine whether the date is the format of the 1988-1-1 function isDate (date) {//Lookup separator index1 = Date.indexof ("-");
If the delimiter does not exist, it is not a valid time if (index1 = 1) return false;
Year of acquisition time = date.substring (0,INDEX1);
Gets the remainder of the time date = date.substring (index1+1);
Find the second delimiter index1 = Date.indexof ("-");
If no second delimiter exists, it is not a valid time if (index1 = 1) return false;
Gets the month month = date.substring (0,INDEX1) in the time;
Gets the day of the time = Date.substring (index1+1); Judge whether it is a number, if not, it is not legal time if (isnumber) && isnumber (month) && Isnumber (day)) {//judge the base scope if (Ye ar<1900 | | year>9999 | | month<1 | | Month >12 | |
DAY<1) return false; Judge 31 days of the Month if (month==1 | | month==3 | | | month==5 | | month==7 | | month==8 | | month==10 | | month==12) && DAY>
return false;
Determine the 30-day Month if ((month==4 | | month==6 | | | month==9 | | month==11) && day>30) return false; If it is February, determine if the run year if (month==2) {if (year%400==0 | |
(year%4==0 && year%100!=0))
{if (day>29) return false;
}else {if (day>28) return false;
'} ' else return false;
return true;
//Determine if email function isemail (email) {if (email.length==0) return false;
index1 = Email.indexof (' @ ');
Index2 = Email.indexof ('. '); if (Index1 < 1//@ symbol not present, or in first position | | Index2 < 1//. Symbols do not exist, or in the first position | | Index2-index1 <2//. At the left of @ or adjacent | |
Index2+1 = = email.length)//. There's nothing behind the symbol return false else return true; } </script>
7, the main mode of output information
1) out.println ("");
Out is an internal object that can be used directly, but must be used within the script (<%%>). Try to use less.
2) Direct output
If it is static information, it can be used directly in the HTML language. Contains HTML tags.
3 expression <%= start, end with%>
For example: <%= "Using the information output from an expression"%>
4 Expression Language (EL)
To focus on mastery.
Basic format: Start identity ${end Identity}
You can output a variety of information: string-type information, objects, error messages.
8, the use of annotations
Page comments: <!--HTML annotation-->
Java comments://Single line comment//Multiline comment
JSP comments: <%--JSP comments--%>
9, in the client to verify, in the server section is required to verify?
Need.
Reason: Clients can bypass client-side validation without directly accessing the processing file through the input interface, which can be problematic if not validated by the server.
Validation at the client is primarily a form-side validation, something that must be validated on the server side.
10, Jstl overview
The standard tag library is a common feature that is not used in Java code and is implemented using tags. The goal is to not show any Java code in the paging file.
Part of the standard tag library: Jstl.jar and Standard.jar two compression packs.
How to use the standard tag library:
1 The first need to put two compressed packets under the Web-inf/lib, two compression package is the tag library implementation file and description file.
2 in the page you need to first declare:
Copy Code code as follows:
<%@ taglib prefix= "FMT" uri= "Http://java.sun.com/jsp/jstl/fmt"%>
Declared through the <%@ taglib%>. The URI property indicates the URI (uniquely identified) of the tag library to use.
Prefix is equivalent to this alias, used in the later use of this alias.
3 call tag in tag library
Copy Code code as follows:
<fmt:requestencoding value= "gb2312"/>
Call the tag by "Alias + tag name" and set the corresponding property.
I hope this article will help you with the JSP program design.