Connect to MySQL and use JSP for simple login operations

Source: Internet
Author: User

Login. jsp

 1 <% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> 2 <HTML> 3 <Head> 4   5   6 </Head> 7   8 <Body> 9 <Center> 10 <H1> login operation! </H1> 11 </Center> 12 <HR> 13 <Form action = "login_check.jsp" method = "Post"> 14 <Table border = "1"> 15 <Tr> 16 <TD colspan = "2"> User Logon </TD> 17   18 </Tr> 19 <Tr> 20 <TD> Login ID: </TD> 21 <TD> <input type = "text" name = "ID"> 22 </TD> 23 </Tr> 24   25 <Tr> 26 <TD> logon password: </TD> 27 <TD> <input type = "password" name = "password"> 28 </TD> 29 </Tr> 30   31 </Tr> 32   33 <Tr> 34 <TD colspan = "2"> <input type = "Submit" value = "Login! "> 35 <TD colspan = "2"> <input type = "reset" value = "reset! "> 36 </TD> 37 </Tr> 38 </Table> 39   40 </Form> 41 </Body> 42 </Html>

 

 

Login_check.jsp

 

 1 <% @ Page contenttype = "text/html" pageencoding = "UTF-8" %> 2 <% @ Page import = "Java. SQL. *" %> 3   4   5 <HTML> 6 <Head> 7 </Head> 8   9 <Body> 10 <Center> 11 <H1> login operation 12 </Center> 13 <HR> 14 <%! //  Define several database connection Constants  15 Public static final string dbdriver = "org. gjt. Mm. MySQL. Driver" ;  16 Public static final string dburl = "JDBC: mysql: // localhost: 3306/zhw" ;  17 Public static final string dbuser = "root" ;  18 Public static final string dbpass = "root" ;  19       Boolean Flag = False  ;  20 String name = Null  ;  21 %> 22 <% 23 Connection conn = Null ; //  Database Connection 24 Preparedstatement pstmt = Null ; //  Database preprocessing  25 Resultset rs = Null ; //  Query the result set to be processed  26 %> 27 <% 28    29     Try  {  30    31 %> 32 <% 33   Class. forname (dbdriver );  34 Conn = Drivermanager. getconnection (dburl, dbuser, dbpass );  35         36 String SQL = "Select name from user where userid =? And Password =? " ;  37 Pstmt = Conn. preparestatement (SQL );  38 Pstmt. setstring (1, request. getparameter ("ID" ));  39         40   41 Pstmt. setstring (2, request. getparameter ("password" ));  42   43 Rs = Pstmt.exe cutequery ();  44   45          If (Rs. Next ()){ //  If data exists, execute  46  47 Flag = True ; //  Login successful  48 Name = Rs. getstring (1 );  49              50   }  51 System. Out. println ("22 if data exists" );  52 %> 53   54 <%55 } Catch  (Exception e ){  56   E. printstacktrace ();  57 } Finally  {  58          Try  {  59   Rs. Close ();  60   Pstmt. Close ();  61  Conn. Close ();  62 } Catch  (Exception e ){  63          64   E. printstacktrace ();  65   }  66      67   }  68      69 %> 70   71 <%72           If (Flag = True  ){  73 %> 74 <JSP: Forward page = "login_success.jsp"> 75 <JSP: Param name = "uname" value = "<% = Name %>"/> 76 </Jsp: Forward> 77   78 <% 79 } Else  { 80 %> 81   82   83 <JSP: Forward page = "login_failure.jsp"/> 84   85   86 <% 87            88   }  89           90 %> 91   92 </Body> 93 </Html>

 

 

Login_success.jsp

 1 <% @ Page contenttype = "text/html" pageencoding = "GBK" %> 2 <HTML> 3 <Head> 4   5   6 </Head> 7     8 <Body> 9 <Center> 10 <H1> login operation  11 <H2> login successful </H2> 12 <H3> welcome !!!! <Font color = "red"> <% = request. getparameter ("uname") %> </font>  13       14 </Center> 15 </Body> 16 </Html>

 

 

Login_failure.jsp

 

  1  <% @ page contenttype =" text/html "pageencoding =" GBK "%>  2   3    4    5   6   7    8  
  
    9   
    10  
    
     11  
     12  
    

Login Failed !!!!!!

13

click to log on again !!!!!!!!

14 15 16

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.