jsp連接MySQL實現登錄


1、下載驅動,並把jar包放到Tomcat的lib目錄下  下載連接

 

2、把jar包添加到項目中

 

3、登錄頁面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <form method="POST" name="frmLogin" action="LoginServlet">
        <h1 align="center">用戶登錄</h1>
        <br /> 賬號:<input type="text" name="username" size="20" maxlength="20" /><br>
        密碼:<input type="password" name="pwd" size="20" maxlength="20" /><br>

        <input type="submit" name="Submit" value="提交"
            onClick="return validateLogin()" /> <input type="reset" name="Reset"
            value="重置" />

    </form>
    <script language="javascript">
   function validateLogin(){
    var sUserName = document.frmLogin.username.value ;
    var sPassword = document.frmLogin.password.value ;
    if (sUserName ==""){
     alert("請輸入用戶名!");
     return false ;
    }
    
    if (sPassword ==""){
     alert("請輸入密碼!");
     return false ;
    }
   }
  </script>
</body>
</html>

4、Servlet代碼連接數據庫

package me.letterwish.servlet;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到驅動 ");
        }
        // 連接URL                    服務器地址                       端口號    數據庫名
        String url = "jdbc:mysql://120.25.248.101:3306/letterwish";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {                                                    //數據可的登錄名  登錄密碼
            conn = (Connection) DriverManager.getConnection(url, "root", "root");
            stmt = (Statement) conn.createStatement();
            // SQL語句
            String sql = "select * from users where account='" + username + "' and pwd= '" + pwd + "'";
            rs = stmt.executeQuery(sql);// 返回查詢結果
        } catch (SQLException e) {
            e.printStackTrace();
        }
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        try {
            if (rs.next()) {
                // 如果記錄集非空,表明有匹配的用戶名和密碼,登陸成功
                response.sendRedirect("home.jsp");
            } else {
                session.setAttribute("message", "用戶名或密碼不匹配。");
                System.out.println("XXXXXXXXXX");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM