栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录


    
账号:
密码:

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文件中添加如下代码


        a
        servlet.LoginServlet
    
    
        a
        /LoginServlet
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号