当前位置: 首页 > 图灵资讯 > 技术篇> Java判断用户名密码 javaweb判断用户名和密码

Java判断用户名密码 javaweb判断用户名和密码

来源:图灵教育
时间:2023-05-18 09:17:33

验证帐号和密码是否正确

一、注册功能

首先,在验证账号和密码是否正确的前提下,需要先注册账号。如果没有账号,就进不去,也无法验证。

事实上,注册功能是一个添加功能,模仿我的第一篇文章,将数据添加到数据库中。

Java判断用户名密码 javaweb判断用户名和密码_session

我在这里做的注册页面比较简单,只输入一个账号和密码,插入数据库,不确认密码,也不确定哪个账号是管理员还是读者。

期末考试快到了,没时间写,考完就补上了。

Java判断用户名密码 javaweb判断用户名和密码_session_02

二.servlet层

package servlet;import java.io.IOException;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 javax.servlet.http.HttpSession;import Bean.xinxi;import dao.dao;import daoimpl.daoimpl;/** * Servlet implementation class login_servlet */@WebServlet("/login_servlet")public class login_servlet extends HttpServlet {    private static final long serialVersionUID = 1L;    dao gooddao=new daoimpl();    /**     * @see HttpServlet#HttpServlet()     */    public login_servlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        doPost(request, response);    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub            request.setCharacterEncoding("UTF-8");            response.setCharacterEncoding("UTF-8");            String studentid=request.getParameter("studentid");            String password=request.getParameter("password");            String method=request.getParameter("method");            String form=request.getParameter("form");            HttpSession session=request.getSession();            ////将数据存储在session中            xinxi good=new xinxi(studentid,password);            good.setStudentid(studentid);            good.setPassword(password);             if("login".equals(method)) {            if(gooddao.login(good)) {//dao层法            session.setAttribute("good", good);//session ///将帐户密码存储在session中            if(form.equals(读者)request.getRequestDispatcher("reader.jsp").forward(request, response);            else if(form.equals(管理员)request.getRequestDispatcher("genlist.jsp").forward(request, response);                }            else {                request.setAttribute(message1”, “用户名或密码错误!");                  request.getRequestDispatcher("denglu.jsp").forward(request, response);            }                   }                   }    }

三.dao层

判断帐号和密码

public boolean login(xinxi n) {        Connection conn = DBUtil.getConnection();        ResultSet rs = null;        PreparedStatement pstmt=null;        String sql = "select * from t_user where studentid =? and password=?";//在表中查询帐户和密码,如果对应,则帐户密码正确,否则,密码错误           boolean isLogin=false;        try {            conn=DBUtil.getConnection();            pstmt=conn.prepareStatement(sql);            pstmt.setString(1,n.getStudentid());                    pstmt.setString(2,n.getPassword());            rs=pstmt.executeQuery();            while(rs.next()) {             isLogin=true;            }                } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return isLogin;    }

三.jsp层

在页面上,我们需要知道哪个用户在使用系统,我们需要使用session来存储用户名。

<%xinxi good=(xinxi)request.getSession().getAttribute("good"); %>已存储在//servlet中的用户名,只需在jsp中获取当前登录用户:<%=good.getStudentid() %>

写下你的jsp页面

结果如下:

 

继续按步骤写功能!!