JSP training (4) -- instance: logon function, JSP: Forward tag, JSP: Include tag, include command, El, C: If

Source: Internet
Author: User

Objective: l further understand JSP applications; l understand the use of expression languages; l master the use of standard tag libraries; l master the use of <JSP: Forward> labels; l master <JSP: include> label usage; l master the use of the <% @ include command. Content: l through the login function, I will further understand JSP technology and introduce Expression Language and jstl usage during development; l modify the code on this basis and understand the usage of related labels. 1. To complete the login function, you must first enter the login information interface (user name and password), and then process the file to complete the judgment, the processing file is used to determine whether the logon information is valid. If the logon succeeds, use the logon success interface to respond. If the logon fails, the logon Failure interface should be used exclusively. Assume that the logon is successful if the username is zhangsan and the password is Lisi. Otherwise, the logon fails. 2. First write the logon interface <% @ page contenttype = "text/html; charset = gb2312 "%> log on to <br> <form name =" form1 "method =" Post "Action =" login_process.jsp "> User ID: <input type = "text" name = "userid"> <br> password: <input type = "password" name = "userpass"> <br> <input type = "Submit" value = "Logon"> <input type = "reset" value = "Reset "> </form> note: client verification is not performed here, mainly focusing on other content. In actual processing, verification should be performed as long as the input information is involved. After a user inputs information, the user needs to verify the information entered by the user to determine whether the user input information is correct. Here, the Java code is not embedded and the jstl label is used to complete the judgment, the following describes how to judge. 3. How to judge? In the jstl core tag Library (CORE), an if tag is used to complete the judgment. The usage is as follows: 1) Two compressed packages jstl must be added. jar and standard. jar to the WEB-INF/LIB; 2) Declare the core tag library <% @ taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core#%> 3) use if to mark the basic syntax format: <C: if test = "[Var =" scope = "]> the code to be executed when conditions are met </C: If> the test attribute is required, it is the judgment condition of if. The VaR attribute and scope attribute are optional. If you want to save the judgment result, you can use the VaR attribute and scope attribute, the VaR attribute specifies the name of the saved variable, and scope specifies the scope of the variable. The content of test should be the value of the expression language whose result is true or false. To use the expression language, the following describes the basic usage of the expression language. 4. Expression Language (EL) Basic Format: Start to mark "$ {" End mark "}" in the middle of the content can be an object, the attribute of an object, calculation Result of several objects. Example (Value in the access request parameter): to operate the userid entered by the user, you can use $ {Param. userid} is equivalent to the following code: <% string userid = request. getparameter ("userid"); out. println (userid); %> example (accessing the attributes of an object): If you want to operate on the attributes of an object, assume that the object user (type: User) exists in the request ), the user has an attribute userid. to directly display this userid, use el to write $ {user. userid}, which is equivalent to the following code: <% USER user = request. getattribute ("user"); string userid = user. getuserid (); out. println (userid); %> Note: You can use brackets to access attributes. For example, you can use the following code to access userid: $ {user ["u Serid "]} can only be used in two cases: l if the attribute contains a special symbol, such as". "; l if the attribute to be accessed is a variable. Example (in Expression Language): If you want to perform an operation: ${3 + 5}, which operations can be used in El are described below. 5. What operations can be performed in El? Common Operations: 1) mathematical operations +-*/% 2) relational operations >===<! = 3) logical operations & |! 6. How to Write this processing program login_process.jsp to prompt when the logon is successful or fails. <% @ Page contenttype = "text/html; charset = gb2312" %> <% @ taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %> <C: if test = "$ {Param. userid =/"zhangsan/" & Param. userpass =/"Lisi/"} "> logon successful! </C: If> <C: If test = "$ {Param. userid! =/"Zhangsan/" | Param. userpass! =/"Lisi/"} "> logon failed! </C: If> different prompts are displayed when logon fails or is successful. However, under normal circumstances, a special page is needed for response. The following describes the usage. 7. On the server side, how does one redirect between pages? A tag <JSP: Forward> is provided in JSP to complete the jump. Syntax format: <JSP: Forward page = "target file"/> the page attribute specifies the file to jump. Purpose: when the file executes this label, it will switch to the target file, and the content after this label will not be executed. For example, there are two files. JSP and B. JSP,. the content of the JSP file is as follows: AAAA <JSP: Forward page = "B. JSP "/> aaaaab. the content of the JSP file is bbbbbbbb. JSP, you will get the result: bbbbbbbbb Note: although the content before the <JSP: Forward> label is executed, it is not displayed, and the subsequent content is not executed. The name of the first file is displayed in the address bar, and the content is displayed in the second file. 8. Modify the login processing file <% @ page contenttype = "text/html; charset = gb2312 "%> <% @ taglib prefix =" C "uri =" http://java.sun.com/jsp/jstl/core "%> <C: If test =" $ {Param. userid =/"zhangsan/" & Param. userpass =/"Lisi/"} "> <JSP: Forward page =" success. JSP "/> </C: If> <C: If test =" $ {Param. userid! =/"Zhangsan/" | Param. userpass! =/"Lisi/"} "> <JSP: Forward page =" failure. JSP "/> </C: If> In addition, you need to write success. JSP and failure. jsp9, success. JSP content <% @ page contenttype = "text/html; charset = gb2312" %> logon success page! 10. Failure. jsp content <% @ page contenttype = "text/html; charset = gb2312" %> logon Failure page! Note the end character of the tag. All labels have the start and end signs. For example, <C: If test = "true"> content </C: If> can also be written as: <C: if test = "true"/> In the second method, the TAG body is empty. The HTML code format is not strict, and there can be no Terminator, for example: <input type = "text" name = "userid"> 11, <JSP: the include> label changes <JSP: forward in the above login_process.jsp file to <JSP: Include to view the effect. Syntax format: similar to <JSP: Forward>. function: include. When the program runs the label, it turns to the execution target file. After the target file is executed, the content after the label is executed continues. It is very similar to method call. Difference from <JSP: Forward>: For the content before the tag, the content before JSP: Forward is executed, but not displayed. the JSP: include execution is also displayed. For the content after the tag, JSP: forward is not executed. jsp: Include is executed and displayed. 12. The syntax of the include command is the same as that of the page command: <% @ include file = "target file" %>: contains the target file. Specifically: before running, copy the content of the target file to the location where the include command is located to form a file and then run it. Difference from <JSP: Include>: The file where <JSP: Include> is located and the file to be switched are executed separately. The <% @ include command is during conversion, copy the content of the target file to the current file. Only one file is available during running. Purpose: Make the shared parts of each page into files, such as the Website Logo and copyright contact information. Use the <% @ include> command to include data as needed. For example, you need to display the copyright information on each webpage. At this time, you need to write the copyright information in a separate file. The following is the copyright information file contact. JSP content: <% @ page contenttype = "text/html; charset = gb2312" %> All rights reserved by Everbright Computer Training School Tel: 888888 in login. the code after JSP contains the copyright information is as follows: <% @ page contenttype = "text/html; charset = gb2312 "%> log on to <br> <form name =" form1 "method =" Post "Action =" login_process.jsp "> User ID: <input type = "text" name = "userid"> <br> password: <input type = "password" name = "userpass"> <br> <input type = "Submit" value = "Logon"> <input type = "reset" value = "Reset "> </form> <% @ include file =" contact. JSP "%> if an error occurs:
org.apache.jasper.JasperException: /login.jsp(8,0) Invalid byte 1 of 1-byte UTF-8 sequence.
You need to set the encoding method. Add the following statement on the page file: <% @ page contenttype = "text/html; charset = gb2312" %> reference material: java Web programming basics tutorial
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.