多用户登陆注册
dl.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆界面</title> </head> <body background="image\td.jpg"> <body> <form action="LoginServlet" method="get"> 账号:<input type="text" name="username" value=""><br> 密码:<input type="password" name="password" value="123456"><br> <input type="radio" id="radio-1" name="type" checked value="1" /> <label>顾客</label> <input type="radio" id="radio-2" name="type" value="2" /> <label>房产经纪</label> <input type="radio" id="radio-3" name="type" value="3" /> <label>管理员</label> <br> <input type= "hidden" name="method" value="login"> <input type="submit" value="登录"><br><a style="color:blue" href='stuzc.jsp?' onclick="del()">没有账号?注册一个!</a> </form> </body> </body> </html>
serlvet页面
package Servlet; import Dao.AdminDao; import Dao.GukeDao; import Dao.GuanliDao; import Bean.gl; import Bean.*; import Bean.guk; 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 jakarta.servlet.http.HttpSession; import java.io.IOException; import Dao.stuad; @WebServlet("/LoginServlet") public class Login extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String method=request.getParameter("method"); if("login".equals(method)) { try { Login(request,response); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }} if("zhuce".equals(method)) { try { Zhuce(request,response); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }} } protected void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String username= request.getParameter("username"); String password = request.getParameter("password"); //获取选择登陆对象的类型参数 int type = Integer.parseInt(request.getParameter("type")); //验证码验证 //验证码验证通过,对比用户名密码是否正确 switch (type) { case 1:{ GukeDao gukeDao = new GukeDao(); guk guke = gukeDao.login(username,password); gukeDao.closeCon(); if (guke == null) { response.getWriter().write("顾客登录失败"); response.setHeader("refresh","2;URL=dl.jsp"); return; }else { HttpSession session = request.getSession(); session.setAttribute("user", guke); session.setAttribute("userType", type); response.getWriter().write("顾客登录成功,2s后返回顾客页面"); response.setHeader("refresh","2;URL=gkzhu.jsp"); } break; } case 2:{ AdminDao adminDao = new AdminDao(); adm admin = adminDao.login(username,password); adminDao.closeCon(); if(admin == null){ response.getWriter().write("房产经纪登录失败"); response.setHeader("refresh","2;URL=dl.jsp"); return; }else{ HttpSession session = request.getSession(); session.setAttribute("user", admin); session.setAttribute("userType", type); response.getWriter().write("房产经纪登录成功,2s后返回页面"); response.setHeader("refresh","2;URL=jingji.jsp"); } break; } case 3:{ GuanliDao guanliDao = new GuanliDao(); gl guanli = guanliDao.login(username, password); guanliDao.closeCon(); if(guanli == null){ response.getWriter().write("管理员登录失败"); response.setHeader("refresh","2;URL=dl.jsp"); return; }else { HttpSession session = request.getSession(); session.setAttribute("user", guanli); session.setAttribute("userType", type); response.getWriter().write("管理员登录成功,2s进入管理员登录·页面"); response.setHeader("refresh","2;URL=admin.jsp"); } break; } default: break; } } protected void Zhuce(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //设置响应类型,并防止中文乱码 stuad A =new stuad(); int i = 0; String username= request.getParameter("username"); String password = request.getParameter("password"); String idnumber= request.getParameter("idnumber"); String user= request.getParameter("user"); String phone= request.getParameter("phone"); String sex= request.getParameter("sex"); String name= request.getParameter("name"); guk B = new guk(); B.setIdnumber(idnumber); B.setName(name); B.setPassword(password); B.setPhone(phone); B.setSex(sex); B.setUser(user); B.setUsername(username); System.out.println(name); try { i=A.add(B); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(i==1) { response.getWriter().append("注册成功!2s后返回登录页面"); response.setHeader("refresh","2;URL=dl.jsp");} else response.getWriter().append("注册失败!"); } }
bean文件这里不做添加,bean文件可以不只有我们所需的username以及password,bean文件可以把所需数据库表中的内容都添加进去,用哪个调用哪个
util文件
package Utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * * @author llq *数据库连接util */ public class DbUtil { private String dbUrl ="jdbc:mysql://localhost:3306/house?serverTimezone=UTC"; private String dbUser = "root"; private String dbPassword = "Zhaowenzhe1007"; private String jdbcName = "com.mysql.jdbc.Driver"; private Connection connection = null; public Connection getConnection(){ try { Class.forName(jdbcName); connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword); System.out.println("数据库连接成功"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("数据库连接失败"); e.printStackTrace(); } return connection; } public void closeCon(){ if(connection != null) try { connection.close(); System.out.println("数据库连接已关闭"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub //创建一个数据库对象 DbUtil dbUtil = new DbUtil(); //连接数据库 dbUtil.getConnection(); } }
Dao文件中的basedao文件
package Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import Utils.DbUtil; public class BaseDao { private DbUtil dbUtil = new DbUtil(); /** * 及时关闭数据库连接,释放资源 */ /** * 基础查询,多条查询 */ public ResultSet query(String sql){ try { PreparedStatement prepareStatement = dbUtil.getConnection().prepareStatement(sql); return prepareStatement.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** *增删改 */ public boolean update(String sql){ try { return dbUtil.getConnection().prepareStatement(sql).executeUpdate() > 0; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public Connection getConnection(){ return dbUtil.getConnection(); } }
举例用的
package Dao; import java.sql.ResultSet; import java.sql.SQLException; import Bean.adm; /** * * @author llq *管理员数据库操作封装 */ public class AdminDao extends BaseDao { //将从表单获取到的username和password作为参数传入,返回一个在数据库的user表中对应了用户名和密码的user对象 /* * 1、如果成功返回一个user对象说明在数据库中找到和表单输入对应的用户名和密码,登录成功 * 2、如果返回为空说明在数据库中没有找到和表单对应的用户名和密码,登录失败*/ public adm login(String username , String password){ String sql = "select * from admin where username = '" + username + "' and password = '" + password + "'"; ResultSet resultSet = query(sql); try { if(resultSet.next()){ adm admin = new adm(); admin.setAgentid(resultSet.getString("id")); admin.setUsername(resultSet.getString("username")); admin.setPassword(resultSet.getString("password")); return admin; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void closeCon() { // TODO Auto-generated method stub } }
根据文件类型修改文件的sql语句即可,多用户登录和单用户登录原理相同,都是采用查找在数据库中进行账号查找,再将密码传出来和输入的密码进行匹配是否相等。而所多便是serlvet中switch语句,根据登录jsp文件传递的type选项,调用不同的语句来进行执行,最终实现登录