我是网络开发的新手,我想请一些帮助。我正在使用Java Eclipse EE,tomcat服务器和mysql进行工资单系统Web应用程序项目。我使用了一个教程并设法在下面创建登录界面。所以现在,当我点击输入我的登录详细信息并点击登录(在localhost:8080 / Payroll)我希望它转到一个网页(我不知道如何创建)并显示一个按钮列表(任意随机)我以后可以重命名的按钮)。有人可以帮帮我吗。我不知道如何使用.JSP,.html,.java,我真的很困惑这些文件类型如何帮助我得到我想要的东西。请帮助别人,我只想让登录按钮重定向到带有按钮的网页。谢谢。
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("**SOME FILE NAME HERE TO REDIRECT TO?**");
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);
}
}
}
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="login" method="post">
<h3>
Employee Login
</h3>
<b>Employee ID:</b> <br>
<input type="text"name="employee_id" size="20"><br><br>
<b>Password:</b><br>
<input type="password" name="password" size="20"><br><br>
<input type="submit" value="Login"><br><br>
</form>
</body>
</html>
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;
}
}
答案 0 :(得分:0)
由于您的index.html已经是“带按钮的页面”,这似乎对您有用。
但是,如果您的页面包含动态数据,那么您最好使用jsp。
您可能希望首先将用户名设置为会话,然后在jsp:
中显示 request.setAttribute("user", employee_id);
RequestDispatcher rs = request.getRequestDispatcher("stackoverflow.jsp");
rs.forward(request, response);
在stackoverflow.jsp
中 <h2><%= request.getAttribute("user") %></h2>
现在比较直接调用jsp和通过登录调用...
确保在Web项目中阅读MVC并考虑使用框架(如Spring),一旦您对概念有所了解。
关于您的代码的一些注意事项:
您永远不会关闭Connection,Statement,ResultSet。考虑一个连接池和try-with-resource:
public static boolean checkUser(String employee_id, String password)
{
boolean st = false;
try (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);
try(ResultSet rs =ps.executeQuery()) {
return rs.next();
}
}catch(Exception e) {
e.printStackTrace();
}
return false;
}