13th Chapter WEB13-JSP Mode &JDBC Advanced Chapter

Source: Internet
Author: User

Today's mission
? Develop a transfer case using the MVC design pattern
Teaching navigation
Teaching objectives
Mastering the design pattern of JSP
Understanding MySQL's transaction management
Mastering the transaction management of JDBC
Management of transactions is done using dbutils
Teaching methods
Case-driven approach
1.1 Previous lesson Content review:
Jsp:

  • Overview of JSPs:
    • Jsp:java Server Pages.
    • The operation principle of JSP: Translate into servlet, compile into class to execute.
  • Scripting elements of the JSP:
    • <%! %>
    • <%%>
    • <%=%>
  • Comments for JSP:
  • Three instructions for JSP:
    • Page,include,taglib <%@ directive Name attribute = "attribute value"%>
  • Built-in objects for JSPs:
    • Pagecontext,request,session,application,page,response,out,config,exception
  • Four scopes of JSP
    • Pagescope: Current Page
    • Requestscope: one request range.
    • Sessionscope: one session
    • Applicationscope: The entire range of applications.
  • Action Tags for JSP:
    • <JSP:FORWARD>,<JSP:INCLUDE>,<JSP:PARAM>,<JSP:USEBEAN>,<JSP:SETPROPERTY>,<JSP: Getproperty>
      EL:
  • Overview of El
    • El:expression Language
    • El's Role:
      • Get Data ${}
      • Perform operations
      • Manipulating objects commonly used in web development ${param}
      • Calling Methods in Java:
        Jstl
  • Overview of Jstl:
    • JSTL:JSP Standard Tag Library.
    • Jstl Tag Library: CORE,FMT,SQL,XML,FN
    • Jstl's core Tag library:
      • If,foreach
    • JSTL Library of functions:
      • ${FN:}
        1.2 Example of using MVC design mode to complete a transfer: 1.2.1 Requirements:
        Design a page, enter three values, one is the payer, one is the payee, and the other is the amount of the transfer. Cannot appear the payer's money is deducted and the payee does not receive the money the situation occurs. and use the MVC design pattern.
        1.2.2 Analysis: The development mode of 1.2.2.1 JSP:
        "Development of Dynamic Web development model"

        "JSP Development Mode One": Understanding
        JSP + JavaBean
  • The process of demonstrating mode one:
    • Some JSP tags are provided in mode one development:<jsp:usebean>, <jsp:setproperty >,<jsp:getProperty>
  • Use the pattern one for a simple test:
    <%
    Receive data:
    / String username = request.getparameter ("username");
    String Password = request.getparameter ("password");
    Encapsulating data:
    User user = new user ();
    User.setusername (username);
    User.setpassword (password);
    /
    %>
    <jsp:usebean id= "user" class= "com.itheima.demo1.domain.User" scope= "page" ></jsp:useBean>
    <%--<jsp:setproperty property= "username" name= "user"/>
    <jsp:setproperty property= "password" name= "user"/>--%>
    <jsp:setproperty property= "*" name= "user"/><!--the value of the element of the form is consistent with the name of the attribute in user and can be automatically encapsulated-
    <jsp:getproperty property= "username" name= "user"/>
    "JSP Development Model II": Mastering
    JSP + Servlet + JavaBean is called the design pattern of MVC.
    Mvc:
    M:model: Model Layer
    V:view: View Layer
    C:controller: Control layer

    "Reflection Technology in Java" (mastering)
    ? Reflection:

    ? Code:
    "Introspective Technology in Java" (learn)
    ? Introspection: The Get or set method used to obtain the properties and attributes of a javabean.
    JavaBean: Is a Java class that satisfies a particular format:
  • You need to provide a parameterless construction method:
  • Property Private
  • The
  • provides the public Get/set method for the private property.
    ? Introspective code:
    public void Demo1 () throws exception{
    //Get information about the bean
    BeanInfo BeanInfo = Introspector.getbeaninfo ( User.class); The properties of the
    //Get Bean Describe the
    propertydescriptor[] pds = Beaninfo.getpropertydescriptors ();
    for (PropertyDescriptor Pd:pds) {
    System.out.println (Pd.getname ());
    / Pd.getreadmethod ();//Get Get Method
    Pd.getwritemethod ();//Get Set method.
    /}
    }
    ? Use introspection to encapsulate a mybeanutils:
    public class Mybeanutils {
    public static void populate (Object obj,map< String,string[]> map) throws exception{
    //Get the name of all the properties of the class:
    BeanInfo BeanInfo = Introspector.getbeaninfo ( Obj.getclass ());
    //Get all the properties in the class:
    propertydescriptor[] pds = Beaninfo.getpropertydescriptors ();
    for (PropertyDescriptor Pd:pds) {
    if (Map.containskey (Pd.getname ())) {
    method = Pd.getwritemethod ();
    Execute Set Method:
    Method.invoke (obj, Map.get (Pd.getname ()) [0]);
    }
    }
    }
    }
    "Overview of transactions"
    ? What is a transaction:
  • A transaction is a logical set of operations in which the individual logical units that make up this set of operations either succeed together or fail together.
    ? Management of MySQL's Transactions: (understanding)
  • Create a table of accounts:
    Create Database web_13;
    Use web_13;
    CREATE TABLE Account (
    ID int primary KEY auto_increment,
    Name varchar (20),
    Money double
    );
    Insert into account values (NULL, ' Jason ', 10000);
    Insert into account values (null, ' chicken sister ', 10000);
    Insert into account values (null, ' Like Flower ', 10000);
    There are two ways to manage MySQL's transactions: (MySQL database transactions are automatically committed by default.) The Oracle database transaction is not automatically committed by default.)
  • 1. Open a transaction manually
  • Start transaction; --Open transaction
  • More than one SQL;
  • Commit/rollback;
  • 2. Set an auto-submit parameter
  • Show variables like '%commit% '; --View the parameters associated with the commit.
  • Set autocommit = 0; --Set the autocommit parameter to OFF.

    "Transaction Management in JDBC" (master)
    ? API for Management of JDBC transactions:



1.2.2.2 Step Analysis:
"Step One": Create a page:
Step two: Import JDBC-related jar packages and tool classes.
Step three: Create the package structure.
"Step four": Submit to Servlet-->service-->dao
"Step Five": Page Jump:
1.2.3 Code implementation: 1.2.3.1 Preparation work:
1.2.3.2 Code implementation:
1.2.3.3 Dbutils for transaction management:
? No transaction management:


? There are transaction management:


1.2.4 Summary: 1.2.4.1 transaction Features:
? Atomicity: Emphasizing the indivisibility of affairs.
? Consistency: It is emphasized that the integrity of the data should be consistent before and after the execution of the transaction.
? Isolation: The execution of a transaction should not be disturbed by other transactions.
? Persistence: Once a transaction ends (commits/rolls back) The data is persisted to the database.
1.2.4.2 if the isolation of transactions is not considered, some security issues are raised:
? A class of reading problems:

  • Dirty reads: One transaction reads data that has not yet been committed by another transaction.
  • Non-REPEATABLE READ: One transaction reads the data of an update that has been committed by another transaction, causing inconsistencies in the results of multiple queries in the current transaction.
  • Virtual read/Phantom read: One transaction reads the insert data that has been committed by another transaction, resulting in inconsistent query results multiple times in the current transaction.
    ? A class of write questions:
  • Two types of missing updates are thrown:
    1.2.4.3 solve the read problem raised:
    To set the isolation level of a transaction:
  • READ UNCOMMITTED: Unread. Dirty read, non-repeatable read, virtual read can occur.
  • Read Committed: Reads committed. Avoid dirty reads. But non-repeatable reads and false reads can occur.
  • REPEATABLE READ: repeatable reading. Avoid dirty reading, not repeatable read. But there is a possibility of a false reading.
  • Serializable: serialized. Avoid dirty reading, non-repeatable reading, and virtual reading.
    MySQL Isolation level: REPEATABLE READ Oracle Isolation LEVEL: Read Committed
    1.2.4.4 shows the occurrence of dirty reads:
    ? Open two windows respectively: A, B
    ? View the isolation level of two Windows separately: SELECT @ @tx_isolation;
    ? The isolation level for the Set a window is: READ UNCOMMITTED:
  • Set session transaction ISOLATION level READ UNCOMMITTED;
    ? To open a transaction in two windows, respectively:
  • Start transaction;
    ? To complete the transfer operation in window B:
  • Update account Set money = money-1000 WHERE name = ' Jason ';
  • Update account Set Money = money + + WHERE name = ' chicken sister ';
    ? Query data in a window: (Money has been to the account---dirty Read)
  • SELECT * from Account; --A transaction read the data that the B transaction has not yet submitted.
    1.2.4.5 demo avoids dirty reads, non-repeatable reads occur
    ? Open two windows respectively: A, B
    ? View the isolation level of two Windows separately: SELECT @ @tx_isolation;
    ? The isolation level for the Set a window is: Read Committed:
  • Set session transaction Isolation level Read Committed;
    ? To open a transaction in two windows, respectively:
  • Start transaction;
    ? To complete the transfer operation in window B:
  • Update account Set money = money-1000 WHERE name = ' Jason ';
  • Update account Set Money = money + + WHERE name = ' chicken sister ';
    ? To query in a window:
  • SELECT * from Account; --Avoid dirty reading.
    ? Commit the transaction in the b window:
  • Commit
    ? Query again in the A window:
  • SELECT * from Account; --the transfer was successful. (Non-REPEATABLE READ: One transaction reads data from an update that has been committed in another transaction, causing inconsistencies in multiple query results.)
    1.2.4.6 demo to avoid non-repeatable reads:
    ? Open two windows respectively: A, B
    ? View the isolation level of two Windows separately: SELECT @ @tx_isolation;
    ? The isolation level for the Set a window is: Repeatable READ:
  • Set session transaction ISOLATION level repeatable read;
    ? To open a transaction in two windows, respectively:
  • Start transaction;
    ? To complete the transfer operation in window B:
  • Update account Set money = money-1000 WHERE name = ' Jason ';
  • Update account Set Money = money + + WHERE name = ' chicken sister ';
    ? In a window query:
  • SELECT * from Account; --the transfer was unsuccessful: avoid dirty reading.
    ? Commit the transaction in the b window:
  • Commit
    ? Query again in the A window:
  • SELECT * from Account; --the transfer was unsuccessful: avoid non-repeatable reading.
    1.2.4.7 demo To avoid the occurrence of virtual reads:
    ? Open two windows respectively: A, B
    ? View the isolation level of two Windows separately: SELECT @ @tx_isolation;
    ? The isolation level for the Set a window is: Serializable:
  • Set session transaction isolation level serializable;
    ? Open the transaction separately in a, B, two windows:
  • Start transaction;
    ? Complete an insert operation in the b window:
  • Insert into account values (null, ' Miss Wang ', 10000);
    ? To do a query in a create:
  • SELECT * from Account; --no results were queried.
    ? Commit the transaction in the b window:
  • Commit --a window will display the data immediately.

13th Chapter WEB13-JSP Mode &JDBC Advanced Chapter

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.