使用javaweb进行用户的登录与注册

1.先创建javaweb项目

2.导入jar包

 

3.在web文件中写出主页面

<%--
  Created by IntelliJ IDEA.
  User: BAIYE
  Date: 2023/2/14
  Time: 10:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>测试</title>
  </head>
  <body>
  <h3>欢迎来到web请完成下面操作</h3>
  </body>
<a href="denglu.jsp">登录</a><br/>
<a href="Zhuce.jsp">注册</a>
</html>

 页面样式如下:

 

 4.写出主页面点击登录后跳转的页面

<%--
  Created by IntelliJ IDEA.
  User: BAIYE
  Date: 2023/2/14
  Time: 10:54
  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="Zhuce" method="post">
    姓名:<input type="username" name="User" value=""/><br/>
    密码:<input type="passaword" name="pwd" value=""/><br/>
    电话:<input type="phone" name="pwd" value=""/><br/>
    地址:<input type="address" name="pwd" value=""/><br/>
    <input type="submit" value="注册">
</form>
</body>
</html>

页面样式如下: 

 5.写出登录成功后的页面

<%--
  Created by IntelliJ IDEA.
  User: BAIYE
  Date: 2023/2/17
  Time: 15:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录成功</title>
</head>
<body>
        <li><a>泡面</a><a>够香够辣就是这个味</a></li>
        <li><a>火腿</a><a>够香够辣就是这个味</a></li>
        <li><a>雪碧</a><a>够香够辣就是这个味</a></li>
</body>
</html>

登录成功页面展示: 

 登录失败页面展示:

 6.用户登录页面

package com.li.servlet;

import com.li.op.User;

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

public class Denglu extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Denglu-get...");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Denglu-post...");
        //前端请求(req)后端处理后,最后给前端响应(resp)
        //这三个获取请求的地址相关
        String uri = req.getRequestURI();
        String con = req.getContextPath();
        String serv = req.getServletPath();

        System.out.println(uri);//包含项目名和资源和路径
        System.out.println(con);//项目名称
        System.out.println(serv);//请求的资源路径
        System.out.println("=====================");
        //从请求中获取用户提交的参数(数据)
        req.setCharacterEncoding("utf-8");//设置请求的编码格式为中文
        String User = req.getParameter("User");//根据表单的name属性获取用户输入的值
        String pwd = req.getParameter("pwd");
        resp.setCharacterEncoding("UTF-8");


        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/day?useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "root";

        Connection connection = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        com.li.op.User denglu=null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            System.out.println(connection);
            String sql = "select * from t_user where username=? and password=?";
            pstm = connection.prepareStatement(sql);
            pstm.setObject(1, User);
            pstm.setObject(2, pwd);
            rs = pstm.executeQuery();

            if (rs.next()) {
                denglu = new User();
                int uid = rs.getInt("uid");
                denglu.setUid(uid);
                denglu.setUsername(rs.getString("username"));
                denglu.setPassword(rs.getString("password"));
                denglu.setPhone(rs.getString("phone"));
                denglu.setAddrress(rs.getString("address"));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
        try {
            if (rs!=null){
                rs.close();
            }
            if (pstm!=null){
                pstm.close();
            }
            if (connection!=null){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        }
//        System.out.println("111111"+denglu);
        if (denglu!=null) {
        resp.sendRedirect("zhuye.jsp");
        }else {
            resp.sendRedirect("Denglu01.jsp");
        }
    }

}

7.注册页面:

package com.li.servlet;

import com.li.dao.UserDao;
import com.li.dao.UserDaoImpI;
import com.li.op.User;
import com.li.util.JDBCutil;

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 java.io.IOException;
import java.sql.*;

public class Zhuce extends HttpServlet {
    private Connection con;
    private Object pstm;



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Zhuce-post...");
        System.out.println("注册");
        req.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charest=UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String addrress = req.getParameter("addrress");
        try {
            Boolean zhuceTest=zhuceTest(username,password,phone,addrress);
            if (zhuceTest){
                resp.getWriter().write("成功");

            }else {
                resp.getWriter().write("失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }
        private Boolean zhuceTest(String username,String password,String phone,String addrress) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/day?useSSL=false&serverTimezone=UTC","root","root");
        String sql="insert into t_user(username,password,phone,addrress)values(?,?,?,?)";
        PreparedStatement pstm=connection.prepareStatement(sql);
        pstm.setObject(1,username);
        pstm.setObject(2,password);
        pstm.setObject(3,phone);
        pstm.setObject(4,addrress);
        int i=pstm.executeUpdate();
        if (i>0){
            if (pstm!=null){
                pstm.close();
            }
            if (connection!=null){
                connection.close();
            }
            return true;
        }else {
            return false;
        }
        }
}




  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值