Client verification, common output methods, and basic JSTL usage in the JSP getting started tutorial.

Source: Internet
Author: User
Tags html comment

Client verification, common output methods, and basic JSTL usage in the JSP getting started tutorial.

This article describes the client verification, common output methods, and basic JSTL usage of the JSP getting started tutorial. Share it with you for your reference. The details are as follows:

I. Objectives:

① Master the basic process of client verification;
② Grasp the JSP output information;
3. master the basic usage of JSTL.

Ii. Main content:

① Introduce the basic process of client verification through an instance;
② Introduce the basic methods of JSP output information;
③ Introduce the basic usage of JSTL through instance analysis.

Client-side verification-related code can be seen everywhere on the network and is very common. Therefore, you do not need to write your own code, but you need to know how to use and modify it. The following describes the basic usage process:

1. How to embed JavaScript code

You can use the JavaSript 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 the start mark and the end mark.

2. How to Write JavaScript

Various verification processes exist in the form of methods. The JavaScript method is defined as follows:

Function Method Name (parameter list) {// method body}

Unlike the method definition in Java, a function declaration must define a method without the return value type. Any result can be returned. The parameter list does not need to provide the parameter type. 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 whether it is a digital function isNumber (str) {for (I = 0; I <str. length; I ++) {// if You Want To determine the decimal point, 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 association between form submission and verification methods?

The onsubmit event of the form is used.

<form name="form1" method="post"action="process.jsp" onsubmit="return isValidate(form1)">

The onsubmit = "return isValidate (form1)" section calls the verification method.

Note: At this time, the form submission event is used, and the submit button is used.

You can also change the submit button to a normal button, and then use the onClick event of the button to call the verification method.

4. Obtain the entered information during verification. If yes?

Name of the form until the form element, and then get the value. For example:
Copy codeThe Code is as follows: userid = form. userid. value;
Variables can be directly used without definition.

5. instance: Verify the user name and password in the registration function

<% @ Page contentType = "text/html; charset = gb2312" %> <script language = "JavaScript"> // function isValidate (form) {userid = form. userid. value; if (userid = "") {alert ("User ID cannot be blank"); return false;} else if (userid. length> 8 | userid. length <6) {alert ("the length should be 6-8 digits"); return false;} userpass = form. userpass. value; if (userpass. length! = 8) {alert ("the password length is not 8! "); Return false;} return true;} </script> register <br> <form name =" form1 "method =" post "action =" process. jsp "onsubmit =" return isValidate (form1) "> User ID: <input type =" text "name =" userid "> the user ID is 6-8 characters long. <br> password: <input type = "password" name = "userpass"> the password length is 8 <br> confirm the 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> Hobbies: <input type = "checkbox" name = "fav" value = ""> motion <input type = "checkbox" name = "fav" value = "Music"> music <input type = "checkbox" name = "fav" value = "programming"> programming <br> education level: <select name = "degree"> <option value = ""> Bachelor's degree </option> <option value = ""> Master's degree </option> <option value = "Emy "> specialist </option> <option value =" "> PhD </option> </select> <br> remarks: <textarea name = "comment"> </textarea> <br> <input type = "submit" value = "submit"> <input type = "reset" value = "reset"> </form>

6. Common verification: introduction through the regiest. jsp file

This verification is not the most comprehensive, and is not the best. If you need it, you can search through the Internet, or you can check JavaScript-related books. There are many ready-made JavaScript methods available. In addition, regular expressions can be used for client verification, which is convenient.

The following code is for reference:

<% @ Page contentType = "text/html; charset = gb2312" %> <script language = "JavaScript"> function isValidate (form) {// obtain 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; // determine the length of the user ID if (! MinLength (userid, 6) {alert ("the length of the user ID is less than 6 characters! "); Form. userid. focus (); return false;} if (! MaxLength (userid, 8) {alert ("the length of the user ID is greater than 8 characters! "); Form. userid. focus (); return false ;}// determine the username length if (! MinLength (username, 6) {alert ("the username length is less than 6 characters! "); Form. username. focus (); return false;} if (! MaxLength (username, 8) {alert ("the username length is greater than 8 characters! "); Form. username. focus (); return false ;}// determine the password length if (! MinLength (userpass, 6) {alert ("the password length is less than 6 characters! "); Form. userpass. focus (); return false;} if (! MaxLength (userpass, 8) {alert ("the password length is greater than 8 characters! "); Form. userpass. focus (); return false;} // determine whether the user name and password are the same if (username = userpass) {alert (" the user name and password cannot be the same! "); Form. userpass. focus (); return false;} // verify whether the two passwords are the same if (userpass! = Userpass2) {alert ("the two passwords are different! "); Form. userpass. focus (); return false ;}// verify that the birthday format is correct if (! IsDate (birthday) {alert ("the birthday format is incorrect! "); Form. birthday. focus (); return false ;}// verify that the email format is correct if (! IsEmail (email) {alert ("Incorrect Email format! "); Form. email. focus (); return false;} // verify that the phone number format is correct if (! IsDigital (phone) {alert ("Incorrect phone Number Format"); form. phone. focus (); return false;} // verify that the address length is correct if (! MaxLength (address, 50) {alert ("the address length is greater than 50 characters! "); Form. address. focus (); return false;} return true;} // verify whether the function isNull (str) {if (str. length = 0) return true; else return false;} // verify whether the minimum length function minLength (str, length) {if (str. length> = length) return true; else return false;} // determines whether the maximum length of the function maxLength (str, length) {if (str. length <= length) return true; else return false;} // determines whether it is a digital function isDigital (str) {for (I = 0; I <str. Length; I ++) {// allows the use of a hyphen if (str. charAt (I)> = '0' & str. charAt (I) <= '9' | str. charAt (I) = "-" & I! = 0 & I! = Str. length-1) continue; else return false;} return true;} // determines whether it is an integer function isNumber (str) {for (I = 0; I <str. length; I ++) {// if You Want To determine the decimal point, 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;} // determines whether the date is used, the date format is 1988-1-1 function isDate (date) {// search separator index1 = date. indexOf ("-"); // if the separator does not exist, it is not a valid time if (index1 =-1) return fals E; // obtain year = date in the time. substring (0, index1); // obtain the remaining part of the time date = date. substring (index1 + 1); // find the second separator index1 = date. indexOf ("-"); // if the second separator does not exist, if (index1 =-1) return false; // obtain the month = date in the time. substring (0, index1); // obtain the day = date in the time. substring (index1 + 1); // determines whether it is a number. if not, it is not a legal time. if (isNumber (year) & isNumber (month) & isNumber (day )) {// determine the basic range: if (year <1900 | year> 9999 | mo Nth <1 | month> 12 | day <1) return false; // judge the 31-day month if (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = 12) & day> 31) return false; // determine the 30-day month if (month = 4 | month = 6 | month = 9 | month = 11) & day> 30) return false; // if it is January 1, February, judge whether it is a runner-up 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 Email function isEmail (email) {if (email. length = 0) return false; index1 = email. indexOf ('@'); index2 = email. indexOf ('. '); if (index1 <1 // @ symbol does not exist, or it is in the first position | index2 <1 //. the symbol does not exist or is in the first position | index2-index1 <2 //. on the left of @ or adjacent | index2 + 1 = email. length )//. there is nothing behind the symbol return false else return true ;} </script> 

7. Main output methods

1) out. println ("");

Out is an internal object that can be used directly, but must be used within the script (<%>. Use as few as possible.

2) Direct output

Static information can be directly used in html. Contains HTML tags.

3) expression <% = start and end with %>

Example: <% = "information output using expressions" %>

4) Expression Language (EL)

It is important to master.

Basic Format: Start ID $ {end ID}
Various types of information can be output: string type information, objects, error message information.

8. Usage of annotations

Webpage Note: <! -- Html comment -->
Java Note: // single line comment/**/multi-line comment
JSP comment: <% -- JSP comment -- %>

9. Have you performed verification on the client? Do you need verification on the server segment?

Yes.

Cause: the client can directly access and process files without passing the input interface, so that the client verification can be skipped. If the verification is not performed on the server, the data will be faulty.
The verification on the client is mainly about format verification. Some items must be verified on the server.

10. JSTL Overview

The standard tag library is a common feature that uses tags instead of Java code. The goal is to avoid any java code in the page file.

Components of the standard tag Library: jstl. jar and standard. jar.

How to use the standard tag Library:

1) First of all, we need to put the two compressed packages under the WEB-INF/lib, the two compressed packages are the implementation file and description file of the label library.

2) on the page, declare:

Copy codeThe Code is as follows: <% @ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
Declare through <% @ taglib %>. The uri attribute specifies the uri (unique identifier) of the tag library to be used ).
The prefix is equivalent to the alias used later.

3) mark in the call Tag Library
Copy codeThe Code is as follows: <fmt: requestEncoding value = "gb2312"/>
Call this tag using "alias + Tag Name" and set the corresponding attributes.

I hope this article will help you with JSP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.