gpt4 book ai didi

java - 如何在管理中检查数据库中的真/假值,并相应地重定向? (Web应用程序)

转载 作者:行者123 更新时间:2023-11-29 07:25:52 25 4
gpt4 key购买 nike

我在 Java Eclipse IDE 上有一个基本的登录系统,带有 tomcat 服务器和 mysql 数据库。我下面的代码显示了登录验证。该程序在 mysql 数据库中搜索employee_id 和密码,如果在数据库中找到,它将重定向到页面“Options.html”。如果未找到employee_id和密码,则打印“Employee ID或Password不正确,请重试”。基本上,我想要的是以下内容:

我的表中有一个名为 Admin 的列,其数据类型为 TINYINT。这基本上是一个 boolean 值,因此 1 表示管理员,0 如果不是管理员。我希望验证过程检查用户是否是管理员。如果用户是管理员,我希望它转到Options.html(这是当前的默认值)。如果用户不是管理员,我希望它转到另一个 html 页面。预先感谢,下面是我的文件。

Login.java(servlet)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String employee_id = request.getParameter("employee_id");
String password = request.getParameter("password");
if(Validate.checkUser(employee_id, password)) {
RequestDispatcher rs = request.getRequestDispatcher("Options.html");
rs.forward(request, response);
}
else
{
out.println("Employee ID or Password is incorrect. Please try again.");
RequestDispatcher rs = request.getRequestDispatcher("index.html");
rs.include(request, response);
}
}


}

Validate.java(类)

import java.sql.*;
public class Validate
{
public static boolean checkUser(String employee_id, String password)
{
boolean st = false;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
PreparedStatement ps = con.prepareStatement("select * from employee_login where employeeID = ? and pwd = ?");
ps.setString(1, employee_id);
ps.setString(2, password);
ResultSet rs =ps.executeQuery();
st = rs.next();

}catch(Exception e)
{
e.printStackTrace();
}
return st;
}
}

最佳答案

您的验证类可以有两个单独的函数。

import java.sql.*;
public class Validate
{
public static boolean userIsAdmin(String employee_id, String password)
{
boolean st = false;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
PreparedStatement ps = con.prepareStatement("select * from employee_login where employeeID = ? and pwd = ? AND admin=1");
ps.setString(1, employee_id);
ps.setString(2, password);
ResultSet rs =ps.executeQuery();
st = rs.next();

}catch(Exception e)
{
e.printStackTrace();
}
return st;
}

public static boolean userIsNotAdmin(String employee_id, String password)
{
boolean st = false;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
PreparedStatement ps = con.prepareStatement("select * from employee_login where employeeID = ? and pwd = ? AND admin=0");
ps.setString(1, employee_id);
ps.setString(2, password);
ResultSet rs =ps.executeQuery();
st = rs.next();

}catch(Exception e)
{
e.printStackTrace();
}
return st;
}
}

然后将其添加到您的 main 中:

if(Validate.userIsAdmin(employee_id, password)) { 
RequestDispatcher rs = request.getRequestDispatcher("Options.html");
rs.forward(request, response);
}
else if(Validate.userIsNotAdmin(employee_id, password))
{
RequestDispatcher rs = request.getRequestDispatcher("other.url");
rs.include(request, response);
}
else
{
out.println("Employee ID or Password is incorrect. Please try again.");
RequestDispatcher rs = request.getRequestDispatcher("index.html");
rs.include(request, response);
}

希望有帮助。

关于java - 如何在管理中检查数据库中的真/假值,并相应地重定向? (Web应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com