Realize the registration and login function

  • Create user table

    use newdb3;

    drop table user;

    create table user(id int primary key auto_increment,username varchar(20),password varchar(20))charset=utf8;

  • If you use a database in a project, you need to do the following things

    • Add jar package information for mysql and database connection pools to pom.xml
    • Copy the jdbc.properties configuration file from the previous project
    • Copy the DBUtils tool class from the previous project into the new project
  • To achieve the registration function:

    • Create reg. HTML page. Prepare a text input field and a password field. Change the submission mode to POST and address to RegServlet
    • Create the RegServlet, leaving the doPost method, which sets the character set, then gets the parameters, gets the database connection using custom template code, and saves the resulting user and password to the database using the JDBC code you learned earlier.
  • The project will receive an error when changing the JRE to 1.7. This error does not affect the operation

    How to solve: right-click properties-> Project Facets -> find Java facets 1.5 to 1.7 can be saved

Enabling the MySQL Service

  • Right click on my computer -> Administration -> Services and Applications -> Services -> Find MySQL right click to start

Implement the registration function from scratch

  1. Create a new project Servlet_2_2 and change jar to WAR

  2. Error correction: the longest right click on the longest

  3. Project association Tomcat: Project name Right-click Properties->Targeted Runtimes Tick

  4. Add two JAR package coordinates to the POM.xml file

  5. Put the jdbc.properties configuration file into the new project

  6. Create the cn.tedu package and take the DBUtils from the previous project

  7. Create reg. HTML page. Prepare a text input field and a password field. Change the submission mode to POST and address to RegServlet

  8. Create the RegServlet, leaving the doPost method, which sets the character set, then gets the parameters, gets the database connection using custom template code, and saves the resulting user and password to the database using the JDBC code you learned earlier.

  9. When obtaining the database connection, you need to change the JRE to 1.7, and an error will be reported after the change

    How to solve: right-click properties-> Project Facets -> find Java facets 1.5 to 1.7 can be saved

Login steps:

  1. To create login.html, just change the submission address LoginServlet and change the value of the submit button to login
  2. Create LoginServlet to leave the doPost method, set the character set to obtain parameters, refer to the JDBC login code, required to implement the correct page to return login success! Error returns a wrong username or password to the page

Procedure for querying employee list:

  1. Create a home.html page and add three hyperlinks. Register href=reg. HTML Login href=login. HTML query list href=ListServlet
  2. Create a ListServlet to leave a doGet method that queries employee information from the database and outputs it to the page via printwriter

Steps for deleting an employee:

  1. Add or remove hyperlinks when the ListServlet returns data to the browser href=DelServlet? Empno = Employee ID
  2. Create DelServlet and leave the doGet method to get the passed employee number, get the database connection, write the delete SQL statement to delete the data corresponding to the employee number.

redirect

  • A redirect is an instruction that the server tells the client to request again to the specified path
  • Execution process: When the server performs the redirect method, it returns a 302 status code and a request path to the client. Upon receiving the 302, the browser immediately sends another request to the specified path
  • response.sendRedirect(

    request.getContextPath()+”/ListServlet”);