Reference content is as follows:
<Html>
<Head>
<Title> logon </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Meta http-equiv = "Content-Language" content = "ch-cn">
</Head>
<Body>
<! -- Form is used to extract information entered and submitted by users -->
<Form method = "post" name = "frmLogin" action = "verifylogin. jsp">
<H1 align = "center"> User Logon <Div align = "center"> User Name:
<Input type = "text" name = "txtUserName" value = "Your name"
Onfocus = "if (this. value = 'your name') this. value =''; "> <br> password:
<Input type = "password" name = "txtPassword" value = "Your password"
Onfocus = "if (this. value = 'your password') this. value =''; "> <br>
<Input type = "submit" name = "Submit" value = "submit"> & nbsp;
<Input type = "reset" name = "Reset" value = "reset"> <br>
</Div> </form> </body>
</Html>
Verifylogin. jsp content:
<% @ Page language = "java" contentType = "text/html; charset = gb2312"
PageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. *" %>
<% @ Page import = "java. util. *" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> logon </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
</Head>
<Body>
<Div align = center>
<%
// Obtain the user name
String sUserName = request. getParameter ("txtUserName ");
// Obtain the password
String sPasswd = request. getParameter ("txtPassword ");
// Register the JDBC driver
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
// The Connection parameters are different from the Access parameters.
String url = "jdbc: mysql: // localhost/LearnJSP ";
// Establish a connection
Connection connection = DriverManager. getConnection (url, "root ",
"011124 ");
// SQL statement
String SQL = "select * from userinfo where username = '" + sUserName
+ "'And userpwd ='" + sPasswd + "'";
Statement stmt = connection. createStatement ();
ResultSet rs = stmt.exe cuteQuery (SQL); // return the query result
// If the record set is not empty, it indicates that a matched user name and password exist and the logon is successful.
If (rs. next ())
{
Out. println ("Logon successful! ");
} Else
// Otherwise, the logon fails.
{
Out. println ("the user name does not exist or the password is incorrect! ");
}
Rs. close ();
Stmt. close ();
Connection. close ();
%>
</Body>
</Html> |