2024/02/04

在servlet层来调用DAO层来实现数据库操作

这里根据查询数据库的返回值来选择跳转的页面,在输入的账号或密码错误时可以给出提示。

package com.example.demo;

import bean.Base_InformationBean;
import bean.InfoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value="/login-servlet")
public class LoginServlet extends HttpServlet {
    private String code;
    private String password;
    private Base_InformationBean hhh;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        code=request.getParameter("nameid");
        password=request.getParameter("pwd");
        hhh=new Base_InformationBean();
        hhh.setCode(code);
        hhh.setPassword(password);
        boolean flag= new InfoDAO().Login(hhh);
        if(flag)
        {
            response.sendRedirect("success.jsp");
        }
        else
        {
            request.setAttribute("msg","账号或密码错误");
            try {
                request.getRequestDispatcher("index.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        doGet(request,response);
    }
}

我们要接受提示首先要给前面的<span>值一个初始的变量用于接收。

<span id="msg" style="font-size: 12px;color:red">${msg}</span><br>

 

posted @ 2024-02-04 16:31  伐木工熊大  阅读(3)  评论(0编辑  收藏  举报