[英]How can I print information from a database (retrieved using java code) into html?
我正在用Java Eclipse IDE做一个Web开发项目。 我想知道是否有一种方法可以从数据库中打印信息并将其显示在html上。 在下面的代码中,我有一个登录功能,该功能在mySQL的数据库表中搜索employee_id和密码,然后验证登录。 在该表中,有一个名为FirstName的列,它是员工的名字。 员工登录后如何在首页上打印该员工的名字? (employeeHome.html)
Validate.java
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();
Login.java
if(Validate.userIsNotAdmin(employee_id, password)) {
RequestDispatcher rs = request.getRequestDispatcher("employeeHome.html");
rs.forward(request, response);
employeeHome.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employee Home Page</title>
</head>
<body>
<form action="Welcome" method="post">
<h3>
Employee Home Page
</h3>
<input type="submit" value="View Personal Information" name="vpi">
<br>
<br>
<input type="submit" value="View Expense Claims" name="vec">
<br>
<br>
<input type="submit" value="View Payslips" name="vps">
<br>
<br>
<input type="submit" value="Change Password" name="cp">
</form>
</body>
</html>
我们使用Apache Wicket。 我们已经使用了9年左右,并且已经为金融机构构建了大型企业应用程序。 它可以与许多技术很好地配合使用,并且还具有一个单元测试框架,我们已经使用了很多。 我强烈推荐它。 这是一个链接。 http://wicket.apache.org/
我从未使用过JSF,因此无法担保它,但是很多人都在使用它,它是Java标准。 这些技术中的任何一种都是用于有效解决您需要解决的问题的。
现在,如果您想自己做所有事情,并且不希望有任何框架可以编写Java Servlet并自己输出HTML。 这是一个教程。 网络上有很多。 http://www.tutorialspoint.com/servlets/servlets-first-example
我建议使用框架。 希望我的回答能帮助您根据自己的需要做出明智的决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.