1. 程式人生 > >JavaWeb的初步學習一

JavaWeb的初步學習一

分享圖片 script query link 用戶 gin sheet where etc

在html頁面輸入內容與數據庫驗證是否匹配,即登錄驗證

數據庫設計

技術分享圖片

login.html

<!DOCTYPE html>
<html>
  <head>
    <title>login.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta 
http-equiv="content-type" content="text/html; charset=UTF-8"> <meta http-equiv="Access-Control-Allow-Origin" content="*"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <label>用戶名:</label><input id="nam"
name="name"type="text" /> <label>密碼:</label><input id="pass" name="password" type="password" /> <input id="register" type="button" value="登錄" id="login"/> <input type="button" value="註冊"/> </body> <script type="text/javascript" src="js/jquery-3.2.1.js"
></script> <script type="text/javascript"> $("#register").click(function(){ var username=$("#nam").val(); var password=$("#pass").val(); if(username!=""&&password!=""){ $.ajax({ type:"POST", url:"search", dataType:"JSON", data:{ "username":username, "password":password } }) }else{ alert("請檢查您的輸入!"); } }) </script> </html>

技術分享圖片

search.java

package com.jquery.register;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class search extends HttpServlet {

    public void destroy() {
        super.destroy(); 
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request,response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8"); 
         response.setCharacterEncoding("utf-8");
         Connection conn = null;
         PreparedStatement stmt = null;
         ResultSet rs = null;
         
         String username = request.getParameter("username");
         String password = request.getParameter("password");
         
         System.out.println(username+password);
         String drive = "com.mysql.jdbc.Driver";
         System.out.println("ajax後臺交互成功");  
         try{
             Class.forName(drive);
             conn = DriverManager
                     .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
             System.out.println("數據庫連接成功");
             String sql="select  *  from user_table where name=‘" + username
    + "‘ AND password=‘" + password + "‘";
             
             stmt = conn.prepareStatement(sql);
             
             rs=stmt.executeQuery();
             
             System.out.println("success");
//             while(rs.next()){
//                 String name = rs.getString(1);
//                String password = rs.getString(2);
//                System.out.println(name + ", " + password);
//             }
             if(rs.next()){
                
                 System.out.println("sys");
             }else{
                 System.out.println("error");
             }
             
        out.flush();
        out.close();
    }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                //註意關閉原則:從裏到外
                    if (rs != null) {
                        rs.close();
                    }
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }
    public void init() throws ServletException {
        // Put your code here
    }

}

技術分享圖片

JavaWeb的初步學習一