JavaWeb的初步学习一

Posted 蓉啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb的初步学习一相关的知识,希望对你有一定的参考价值。

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的初步学习一的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb的初步学习一

JavaWeb的初步学习二之数据库连接

译丨Yarn - Javascript 新一代套件管理

20160322 javaweb 学习笔记--response验证码实现

静态网页框架设计首次体验(文章改)

JavaWeb04_Servlet应用初步