This article describes the client verification, common output methods and basic JSTL usage of JSP introductory tutorial. Share it for your reference. The details are as follows:
1. Goal:
① Master the basic process of client verification;
② Master the way of JSP output information;
③ Master the basic usage of JSTL.
2. Main content:
①Introduce the basic process of client verification through examples;
② Introduce the basic way of JSP output information;
③Introduce the basic usage of JSTL through example analysis.
Client verification-related code is everywhere on the network and is very common, so in general, everyone does not need to write it yourself, but you need to know how to use and modify it. The basic usage process is described below:
1. How to embed JavaScript code
Use JavaScript code to complete client verification of user input information. The process of embedding JavaScript code in the page is as follows:
<script language="JavaScript"> // Embed JavaScript code here</script>
JavaScript code must be between this start flag and end flag.
2. How to write JavaScript methods
Various verification processes exist in the form of methods. The definition of JavaScript methods is as follows:
function Method name(Parameter list) { // Method body}
Unlike the method definition in Java, function declares that you want to define a method, and does not need to return a value type, and can return any result. The parameter list does not need to give the type of the parameter. Here is an example of a method:
function validate(form) { … }
Here is a complete method to determine whether a parameter is a number:
// Determine whether it is a numberfunction isNumber(str) { for(i=0;i<;i++) { // If you want to judge the decimal, you need to judge the decimal point if((i)>='0' && (i)<='9' || (i)=="-" && i==0) continue; else return false; } return true; }
3. How to establish the relationship between form submission and verification methods?
Completed using the onsubmit event of the form form.
<form name="form1" method="post" action="" onsubmit="return isValidate(form1)">
Where: onsubmit="return isValidate(form1)" part is a call to the verification method.
Note: At this time, the form submission event is used and the submission button is used.
You can also modify the submit button to a normal button, and then use the button's onClick event to call the verification method.
4. When performing verification, you need to obtain the input information. If so?
The name of the form is until the form element and then the value is obtained. For example:
Variables do not need to be defined and can be used directly.
5. Example: Verify the username and password in the registration function
<%@ page contentType="text/html;charset=gb2312"%> <script language="JavaScript"> // Method for verification function isValidate(form){ userid = ; if(userid==""){ alert("User ID cannot be empty"); return false; }else if(>8 || <6){ alert("The length should be 6-8 bits"); return false; } userpass=; if(!=8){ alert("The length of the password is not 8!"); return false; } return true; } </script> Please register<br> <form name="form1" method="post" action="" onsubmit="return isValidate(form1)"> userID:<input type="text" name="userid">userIDThe length is6-8Bit<br> Password:<input type="password" name="userpass">要求PasswordThe length is8<br> 确认Password:<input type="password" name="userpass1"><br> gender:<input type="radio" name="sex" value="male" checked>male <input type="radio" name="sex" value="female">female<br> Hobby:<input type="checkbox" name="fav" value="sports">sports <input type="checkbox" name="fav" value="music">music <input type="checkbox" name="fav" value="programming">programming<br> Education: <select name="degree"> <option value="Undergraduate">Undergraduate degree</option> <option value="master">master</option> <option value="Specialty">Specialty</option> <option value="PhD">PhD</option> </select><br> Remark: <textarea name="comment"></textarea><br> <input type="submit" value="submit"><input type="reset" value="Reset"> </form>
6. Commonly used verification: through file introduction
This verification is not the most comprehensive and not the best. If you need it, you can search through the Internet. You can also check JavaScript related books. There are many ready-made JavaScript methods to use. In addition, regular expressions can be used when performing client verification, and it is more convenient.
The following code is for reference:
<%@ page contentType="text/html;charset=gb2312"%> <script language="JavaScript"> function isValidate(form) { // Get the information entered by the user userid = ; username = ; userpass = ; userpass2 = form.; birthday = ; email = ; address = ; phone = ; // Determine the length of the user ID if(!minLength(userid,6)) { alert("The length of the user ID is less than 6 digits!"); (); return false; } if(!maxLength(userid,8)) { alert("The length of the user ID is greater than 8 digits!"); (); return false; } // Determine the length of the username if(!minLength(username,6)) { alert("The username length is less than 6 digits!"); (); return false; } if(!maxLength(username,8)) { alert("The username length is greater than 8 digits!"); (); return false; } //Judge password length if(!minLength(userpass,6)) { alert("The password length is less than 6 digits!"); (); return false; } if(!maxLength(userpass,8)) { alert("The password length is greater than 8 digits!"); (); return false; } // Determine whether the user name and password are the same if(username==userpass) { alert("The username and password cannot be equal!"); (); return false; } // Verify that the passwords are the same twice if(userpass != userpass2) { alert("The passwords entered twice are different!"); (); return false; } // Verify that the birthday format is correct if(!isDate(birthday)) { alert("The birthday is incorrect!"); (); return false; } // Verify that the email format is correct if(!isEmail(email)) { alert("The email format is incorrect!"); (); return false; } // Verify that the phone number is in the correct format if(!isDigital(phone)) { alert("The phone number is incorrect"); (); return false; } // Verify that the address length is correct if(!maxLength(address,50)) { alert("The address length is greater than 50 digits!"); (); return false; } return true; } // Verify that it is empty function isNull(str) { if(==0) return true; else return false; } // Verify that the minimum length is met function minLength(str,length) { if(>=length) return true; else return false; } // Determine whether the maximum length is satisfied function maxLength(str,length) { if(<=length) return true; else return false; } // Determine whether it is a number function isDigital(str) { for(i=0;i<;i++) { // Allow hyphen if((i)>='0' && (i)<='9' || (i)=="-" && i!=0 && i!=-1) continue; else return false; } return true; } // Determine whether it is an integer function isNumber(str) { for(i=0;i<;i++) { // If you want to judge the decimal, you need to judge the decimal point if((i)>='0' && (i)<='9' || (i)=="-" && i==0) continue; else return false; } return true; } // Determine whether it is a date, the format of the date is 1988-1-1 function isDate(date) { // Find the delimiter index1 = ("-"); // If the delimiter does not exist, it is not a legal time if(index1 == -1) return false; // Get the year in time year = (0,index1); // Get the rest of the time date = (index1+1); // Find the second separator index1 = ("-"); // If no second separator exists, it is not a legal time if(index1 == -1) return false; // Get the month in the time month = (0,index1); // Get the day in time day = (index1+1); // Determine whether it is a number, if not, it is not a legal time if(isNumber(year) && isNumber(month) && isNumber(day)) { //Judge basic scope if(year<1900 || year>9999 || month<1 || month >12 || day<1) return false; //Judge 31 days of month if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day>31) return false; //Judge 30 days of month if((month==4 || month==6 || month==9 || month==11) && day>30) return false; // If it is February, determine whether it is Runnian or not 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 whether it is an email function isEmail(email) { if(==0) return false; index1 = ('@'); index2 = ('.'); if(index1 < 1 // @ symbol does not exist, or is in the first position || index2 < 1 // .The symbol does not exist, or is in the first position || index2-index1 <2 // .To the left or adjacent to @ || index2+1 == ) // .There is nothing behind the symbol return false else return true; } </script> <html> <head> <title>Registration interface</title> </head> <body> <h2 align="center">Please register</h2> <form name="form1" action="register_confirm.jsp" method="post" onsubmit="return isValidate(form1)"> <table align="center"> <tr><td> userID:</td><td><input type="text" name="userid" value="${}"> </td></tr> <tr><td> userID:</td><td><input type="text" name="userid"> </td></tr> <tr><td> user名:</td><td><input type="text" name="username"> </td></tr> <tr><td> Password:</td><td><input type="password" name="userpass"></td></tr> <tr><td> 确认Password:</td><td><input type="password" name="userpass2"></td></tr> <tr><td> Birthday:</td><td><input type="text" name="birthday">The format is:1988-1-1</td></tr> <tr><td> Education:</td><td> <input type="radio" name="degree" value="Specialty">Specialty <input type="radio" name="degree" value="Undergraduate" checked>Undergraduate degree <input type="radio" name="degree" value="Master's degree">Master's degree <input type="radio" name="degree" value="Doctoral Student">Doctoral student <input type="radio" name="degree" value="other">other</td></tr> <tr><td> area:</td><td> <select name="local"> <option value="East China">East China</option> <option value="South China">South China</option> <option value="North China">North China</option> <option value="northeast">northeast</option> <option value="southeast">southeast</option> <option value="southwest">southwest</option> <option value="northwest">northwest</option> <option value="northeast">northeast</option> <option value="Central China">Central China</option> </select></td></tr> <tr><td> E-mail:</td><td><input type="text" name="email"></td></tr> <tr><td> address:</td><td><input type="text" name="address"></td></tr> <tr><td> Telephone:</td><td><input type="text" name="phone"></td></tr> <tr><td> Remark:</td><td> <textarea rows="8" name="comment" cols="40"></textarea></td></tr> <tr><td> <input type="reset" value="Reset"></td><td> <input type="submit" value="submit"></td></tr> <table> </form> </body> </html>
7. The main way to output information
1)("");
out is an internal object and can be used directly, but must be used within the script (<% %>). Try to use as little as possible.
2) Direct output
If it is static information, it can be used directly in the html language. Contains HTML tags.
3) Expression <%= starts, ends with %>
For example: <%="Information output using expression"%>
4) Expression Language (EL)
We must focus on mastering it.
Basic format: Start ID ${ End ID}
Various information can be output: string type information, object, and error prompt information.
8. Usage of comments
Web page comments: <!-- html comments -->
Java comments: // Single line comments /* */ Multi-line comments
JSP comments: <%-- JSP comments --%>
9. Verification is performed on the client. Does verification need to be performed on the server segment?
need.
Cause: The client can directly access the processing file without the input interface, so that client verification can be skipped. If it is not validated on the server, there will be problems with the data.
Verification on the client is mainly based on format verification, and some things must be verified on the server side.
10. JSTL Overview
The standard tag library is a common function that is implemented without Java code but using tags. The goal is to not appear in the page file any java code.
Components of the standard tag library: and two compressed packages.
How to use the standard tag library:
1) First, two compressed packages need to be placed under WEB-INF/lib. The two compressed packages are the implementation files and description files of the tag library.
2) You need to declare it first in the page:
Declare it via <%@ taglib %>. The uri property indicates the uri (unique identifier) of the tag library to be used.
prefix is equivalent to an alias for this, and this alias is used later.
3) Call marks in the tag library
Call this tag through "Alias + Tag's Name" and set the corresponding attributes.
I hope this article will be helpful to everyone's JSP programming.