会话追踪:java结合jsp实现增删改查功能

1.第一步创建一个用户数据表

 2.数据库的JDBC连接

public class jdbc extends HttpServlet {

    public static Connection connection() throws ClassNotFoundException,SQLException{

        String url = "jdbc:mysql://localhost:3306/woek?useUnicode=true&characterEncoding=utf-8&useSSL=false";

        String driver = "com.mysql.jdbc.Driver";

        String username = "root";

        String password = "Hcz3108190431";

        Class.forName(driver);

        java.sql.Connection connection = DriverManager.getConnection(url, username, password);

        return connection;
    }
}

3.结合数据库编写登陆代码

package woek;

import JDBC.JDBC;
import Servlet.HttpServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


@WebServlet("/zhu")
public class session extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");

        String password = req.getParameter("password");

        System.out.println(username);

        System.out.println(password);

        try {
            Connection connection = jdbc.connection();

            Statement statement = connection.createStatement();

            String sql = "SELECT * FROM user WHERE username='"+username+"' AND password='"+password+"'";

            ResultSet resultSet = statement.executeQuery(sql);

            if (resultSet.next()){

                req.setAttribute("username",username);
                req.setAttribute("password",password);

                req.getRequestDispatcher("/woek/woek2.jsp").forward(req,resp);

            }else{

                req.getRequestDispatcher("/woek/woek3.jsp").forward(req,resp);

            }

        } catch (ClassNotFoundException e) {

            throw new RuntimeException(e);

        } catch (SQLException e) {

            throw new RuntimeException(e);
        }
    }
}

 4.jsp登陆页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/6/4
  Time: 14:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<c:if test="${requestScope.username!=null}">
    <h1>登陆成功</h1>
</c:if>
<c:if test="${requestScope.username==null}">
   <%
       response.sendRedirect("/woek/woek3.jsp");
   %>
</c:if>//跳转



<a href="/woek/Mod.jsp"><input type="button" value="修改密码"></a>//修改


</body>
</html>

结果

5.修改密码

package woek;

import Servlet.HttpServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;


@WebServlet("/zhu3")
public class Modification extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String pasword=req.getParameter("pasword");

        String password=req.getParameter("password");

        if (pasword.equals(password)){

            System.out.println("已修改密码为"+password);

            req.getRequestDispatcher("/woek/new.jsp").forward(req,resp);
        }
            try {

                Connection connection=  jdbc.connection();

                Statement statement = connection.createStatement();

                statement.executeUpdate("UPDATE user SET password="+password);



            } catch (SQLException e) {

                throw new RuntimeException(e);

            } catch (ClassNotFoundException e) {

                throw new RuntimeException(e);
            }
        }
    }

6.修改密码的jsp页面

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/6/7
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<form action="/zhu3" method="post">
    <br>
    新密码:<input type="text" name="pasword">
    <br>
    确认密码:<input type="text" name="password">
    <br>
    <button type="submit">确认修改</button>
</form>
</body>
</html>

 结果

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值