Servlet & Jsp dynamic website development_6. JavaWeb simple login implementation

After knowing the forwarding and redirection, we can make some small demos in combination with the JDBC we learned before.

One, log in

We know that this login function is available on many websites. Of course, there are also registrations. Let's make a simple login first and feel the feeling

We first open the database, create a test1 database, and then we create a table, the name of the table is called t_user

CREATE TABLE `t_user`  (
  `id` int(0) NOT NULL,
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

Here is the script for everyone, username is the login account, password is the password, and name is the name and nickname of the person who logs in

Then we can create a new project or not, just use the previous HelloWorld, I will not create a new project here~~

With the database, we need to create a Servlet, this Servlet is used to process user login requests

After having this Servlet, we need to do some operations in doPost

1. We need to obtain the user's login account and password

2. Go to the database to compare, do the account and password match?

3. If it is correct, we will let the user jump to the correct page. If it is not correct, we will send the request to the error page, telling the user that the account password is wrong

Then we operate according to the idea, first use the getParameter() method to get the username and password

Then we need to go to the database for comparison. Here, we need to introduce this jdbc jar package. If you have it, you can use it directly. If you don’t have it, you can go to the maven warehouse to download it (maven warehouse address: https://mvnrepository .com/ )

That's it, then click in

I downloaded it against my local database version. I used 8.0.19 locally, so I downloaded the 8.0.19 jar

Just click on this jar, you can see 2.2MB, not very big

After downloading, we copy this jar and paste it into our project

Need to paste into lib, don't paste it indiscriminately! ! !

Then you can see

There is one in this place, which means that we can use jdbc in this project. If not, we need to build it by ourselves. If there is, don't build it! ! !

Then we encapsulate a DBUtil class by ourselves to facilitate our operations in the future, as follows

I am using the MySQL 8.x version here, that is to say, the jar package structure for connecting to the database has been changed, so my Class.forName is written as com.mysql.cj.jdbc.Driver. If you are Used before 5.7, the driver package is com.mysql.jdbc.Driver

Of course, the time zone is also added in 8.x, so my url is relatively long, and everything else is the same as before, and then we write a main method to test this

After OK, let's go back to our LoginServlet, and upload the code directly here, so we won't take screenshots one by one. If not, you can go to my JDBC tutorial: https://blog.csdn.net/weixin_45908370/category_10229180 .html

We get the password through the account. If the password is missing or wrong, we let it jump to the wrong page. If there is no problem, then we jump to the correct page.

Then let's just let it go, let's create the page, and come back to write this~~

Let's get a login.jsp first

Here we fill in the request address in our action. Here we request the /login method in our project. This must be written here. If you don’t write this way, a 404 will appear. We will verify it for everyone later, and then the method. This is Submit type, we specify post

Here, after our form is submitted, we will get the value according to the name attribute in the input tag later, so we need to contact our backend

These two things correspond, if they don’t correspond, we won’t be able to get them

Then a button to submit, finish the login page, and then make a successful jump page, and then make an error page

Here we use the EL expression to output an errorInfo information

Then configure our web.xml

Finally, we go back to perfect the LoginServlet method

Attention here! To add this return, don’t forget it, otherwise an error will occur~~~

Finally, our database fills in a piece of data

After doing this, let's start tomcat and test it

Request address: http://localhost:8080/HelloWorld/login.jsp

Enter the account password, we first enter a non-existent user

Click to log in, let's see the effect

Then we change to admin and enter the wrong password

Finally, we changed to the correct account password to log in

OK, there is no problem, here you can add some judgments to make this program better, for example, judge whether the password is empty, whether the account is empty, etc.

 

Then let's talk about the issues we should pay attention to. The first is the login request address, we will remove /HelloWorld, and then we will request

As you can see, it's /login now, let's enter the correct account password and see what happens

As you can see, there is a 404 page. Here we pay attention to the address bar. We can find that the /HelloWorld thing is gone. This is why we can't request it. Of course, there will be a framework to help us deal with this thing in the later stage. , But be aware of this problem~~~

 

You can check it out for yourself, if you don’t understand, please contact me QQ: 2100363119

Welcome everyone to visit my website: https://www.lemon1234.com

If you can, pay attention to my official account, it is on my website, updated every day~~~, unlimited resources to enjoy Java, thank you~

Recently, the mini program has also been opened. You can scan the code to have fun.

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/113095744