【无标题】

1.用户登录

<%--
  Created by IntelliJ IDEA.
  User: 小保底不歪
  Date: 2022/6/23
  Time: 15:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<body>
<h1>登录页面</h1>
<form action="/javaweb/user?method=login" method="post">
    <p><input type="text" name="username" placeholder="请输入用户名"></p>
    <p><input type="password" name="password" placeholder="请输入密码"></p>
    <p><input type="submit" value="立即登录"></p>
</form>
</body>
</html>

2.然后就会去servlet层找user?method=login方法

package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.Student;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 *
 * Created by 小保底不歪 
 * 2022/6/23 15:21
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    // 登录
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if ("admin".equals(username) && "123456".equals(password)) {// 登录成功 跳转到主页

            ArrayList<Student> students = new ArrayList<>();

            // 把十条数据放进集合中 在把集合放到session里 index去session拿值回显
            for (int i = 0; i < 10; i++) {
                Student student = Student.builder().stuNo("11" + i).stuName("郭新桥").age(20 + i).birth(new Date()).build();
                students.add(student);
            }
            HttpSession session = req.getSession();
            session.setAttribute("students",students);

            resp.sendRedirect(req.getContextPath() + "/index.jsp");
            
        } else {// 登陆失败 跳转到login 并附带错误信息
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write("用户名或密码错误!");
            writer.close();
        }
    }
}

3.最后跳转到主页回显用户的数据

<%--
  Created by IntelliJ IDEA.
  User: 小保底不歪
  Date: 2022/6/23
  Time: 15:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>主页</title>
</head>
<body>
<h1>主页</h1>
<table border="1px" cellspacing="0" width="100%">
    <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>生日</th>
    </tr>

    <c:forEach items="${sessionScope.students}" var="student">
        <tr>
            <td>${student.stuNo}</td>
            <td>${student.stuName}</td>
            <td>${student.age}</td>
            <td>
                <fmt:formatDate value="${student.birth}" pattern="yyyy-MM-dd"></fmt:formatDate>
            </td>
        </tr>
    </c:forEach>

</table>
</body>
</html>

2.注册验证

<%--
  Created by IntelliJ IDEA.
  User: 小保底不歪
  Date: 2022/6/23
  Time: 15:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-1.11.1.js"></script>
</head>
<body>
<h1>注册页面</h1>
<form action="/javaweb/user?method=login" method="post">
    <p><input type="text" name="username" placeholder="请输入账号"></p><span id="info"></span>
    <p><input type="password" name="password" placeholder="请输入密码"></p>
    <p><input type="submit" value="立即注册"></p>
</form>

    <script>
        $(":text").blur(function () {
            var username = $(this).val()
            $.ajax({
                url:'/javaweb/user?method=checkUserName',
                data:{username:username},
                type:'post',
                dataType:'json',
                success:function (res) {
                    var msg = res.msg
                    $("#info").text(msg)
                }
            })
        })
    </script>

</body>
</html>

2.底层代码

// 检查用户名是否合法
    protected void checkUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        HashMap<String, Object> map = new HashMap<>();
        if("admin".equals(username)){// 不合法
            map.put("code",500);
            map.put("msg","用户名已被占用");
        }else{// 合法
            map.put("code",200);
            map.put("msg","用户名可用");
        }

        // 把信息回显给前端页面
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String s = JSON.toJSONString(map);
        writer.write(s);
        writer.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值