User Logon method 1-full jsp
Logical flowchart:
Login verification. jsp code:
<Body>
<%
String username = request. getParameter ("username ");
String password = request. getParameter ("password ");
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Connection ct = DriverManager. getConnection ("JDBC: ODBC: mysql", "root", "asdasd ");
String ckUserSql = "select password from user where username = '" + username + "'";
Statement st = ct. createStatement ();
ResultSet rs = st.exe cuteQuery (ckUserSql );
If (rs. next ()){
If (password. equals (rs. getString (1 )))
Response. sendRedirect ("success. jsp ");
Else
Response. sendRedirect ("fail. jsp"); // password error
} Else
Response. sendRedirect ("fail. jsp"); // user do not exist
%>
</Body>
User Logon method 2 -- jsp-> servlet-> jsp
Logical flowchart:
The servlet code is basically the same as the jsp verification code.
So what is the difference between the two methods?
A: One is jsp for logical judgment, and the other is servlet for logical judgment! (Nonsense)
This is not nonsense. We should consider the main responsibilities of jsp and servlet. Jsp is used as the rendering layer. It is embedded with some java code snippets in html. Let's look back at the jsp login verification. Nothing in the whole body is displayed for the browser, which loses the advantage of jsp. In addition, jsp eventually needs to be parsed into a servlet by a web container.
Therefore, we should allow each part to take advantage of its own advantages, and do not use servlet for login interface or jsp for logic verification. (Although they can do it)
User Logon method 3 -- jsp-> servlet-> java class-> servlet-> jsp
Logical flowchart:
A java class is specially set up for data verification. The function returns the verification result.
Of course, we should also need a dedicated class to connect to the database and execute SQL statements. (The figure does not show that the above Code is not the same, but we should use a class to do this)
As for why we want to do this, everyone has their own opinions. I will go into details.
This article is from the "walking all the way" blog