jsp+servlet实现的简单登录验证

jsp+servlet连接数据库的登录验证

1.打开IDEA,新建login.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
</form>
    <form method="post" action="LoginServlet">
        账号:<input type="text" name="username" size="20" maxlength="20" id="user"/><br>
        密码:<input type="password" name="pwd" size="20" maxlength="20" id="psd"/><br>
        <button onclick="validateLogin()">提交</button>
        <input type="reset" value="重置" />
    </form>
    <script>
        function validateLogin(){
        var sUserName = document.getElementById("user").value;
        var sPassword = document.getElementById("psd").value;
        if (sUserName==""){
            alert("请输入用户名!");
        }

        if (sPassword==""){
            alert("请输入密码!");
            }
        }
    </script>
</body>
</html>

2.打开MySQL数据库图形化管理软件,创建数据库
在这里插入图片描述
letterwish为数据库,users为表名
在这里插入图片描述
3.在web-inf下创建lib文件夹,将MySQL驱动加载包复制到此文件夹
在这里插入图片描述

4.打开IDEA,在src文件夹下创建servlet包,在包下创建LoginServlet.java类

package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public LoginServlet() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        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.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到驱动 ");
        }
        // 连接URL                    服务器地址                       端口号    数据库名
        String url = "jdbc:mysql://localhost:3306/letterwish";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {                                                    //数据库的登录名  登录密码
            conn =DriverManager.getConnection(url, "root", "2001223");
            stmt =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("index.jsp");
            } else {
                session.setAttribute("message", "用户名或密码不匹配。");
                response.getWriter().write("用户名或密码不匹配");
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

5.配置文件,找到web-inf文件下的web.xml文件中添加如下代码

<servlet>
        <servlet-name>a</servlet-name>
        <servlet-class>servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>a</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙行者ღ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值