我在下面有一个servlet,它从数据库中检索大量信息,然后以表格形式打印出来。我正在使用out.println(html代码)输出此页面上的所有内容。有没有更有效的方法在servlet中编写这个HTML代码?对于我想要添加的每个html元素/属性等,我必须使用out.println(<head>)
作为示例。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class PersonalInfoOutput extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
String employeeid = "";
if(session != null) {
employeeid = (String)session.getAttribute("employeeid");
}
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 employeeID, FirstName, LastName, Admin, DOB, Address, Email, HourlyRate, Gender, ALeaveBalance, SLeaveBalance, ActiveStatus, Role, BSB, BankName, AccNumber, SuperNumber, SuperCompany from payroll_system.employee_info where employeeID = ?");
ps.setString(1, employeeid);
ResultSet rs = ps.executeQuery();
st = rs.next();
if(st){
boolean adminTrue = rs.getBoolean("Admin");
boolean activeTrue = rs.getBoolean("ActiveStatus");
out.println("<html>");
out.println("<head>");
out.println("<style>");
out.println("table { border-collapse: collapse; width: 50%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even){background-color: #f2f2f2}");
out.println("tr:hover {background-color: #e2f4ff;}");
out.println("</style>");
out.println("<link rel = stylesheet type = text/css href = main.css>");
out.println("<link rel = stylesheet type = text/css href = sidebar.css>");
out.println("<title>Personal Information</title>");
out.print("</head>");
out.println("<body>");
if(adminTrue){
out.println("<ul>");
out.println("<li><a class=active >View Personal Information</a></li>");
out.println("<li><a href=xyz>View Expense Claims</a></li>");
out.println("<li><a href=xyz>View Payslips</a></li>");
out.println("<li><a href=changePassAdmin.html>Change Password</a></li>");
out.println("<li><a href=xyz>Maintain Employee Information</a></li>");
out.println("<li><a href=xyz>Maintain Tax Information</a></li>");
out.println("<li><a href=xyz>Maintain Payroll Items</a></li>");
out.println("<li><a href=xyz>Maintain Timesheet</a></li>");
out.println("<li><a href=xyz>Maintain Employee Expenses</a></li>");
out.println("<li><a href=xyz>Run Payroll</a></li>");
out.println("<li><a href=xyz>Generate Reports</a></li>");
out.println("</ul>");
out.println("<div style=margin-left:25%;padding:1px 16px;height:1000px;>");
out.println("</div>");
}
else if(!adminTrue){
out.println("<ul>");
out.println("<li><a class=active href=PersonalInfoOutput>View Personal Information</a></li>");
out.println("<li><a href=xyz>View Expense Claims</a></li>");
out.println("<li><a href=xyz>View Payslips</a></li>");
out.println("<li><a href=.html>Change Password</a></li>");
out.println("</ul>");
out.println("<div style=margin-left:25%;padding:1px 16px;height:1000px;>");
out.println("</div>");
}
out.println("<h1>Personal Information</h1>");
out.println("<table border =1>");
out.println("<tr>");
out.println("<td>Name</td>");
out.println("<td>"+ rs.getString("FirstName") + " " + rs.getString("LastName") + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Employee ID</td>");
out.println("<td>"+ rs.getString("employeeID")+"</td>");
out.println("</tr>");
if(adminTrue) {
out.println("<tr>");
out.println("<td>Admin</td>");
out.println("<td>Yes</td>");
out.println("</tr>");
}
else{
out.println("<tr>");
out.println("<td>Admin</td>");
out.println("<td>No</td>");
out.println("</tr>");
}
out.println("<tr>");
out.println("<td>Date Of Birth</td>");
out.println("<td>"+ rs.getString("DOB")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Residential Address</td>");
out.println("<td>"+ rs.getString("Address")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Email</td>");
out.println("<td>"+ rs.getString("Email")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Hourly Income</td>");
out.println("<td>"+ "$" + rs.getString("HourlyRate")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Gender</td>");
out.println("<td>"+ rs.getString("Gender")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Annual Leave Balance</td>");
out.println("<td>"+ rs.getString("ALeaveBalance")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Sick Leave Balance</td>");
out.println("<td>"+ rs.getString("SLeaveBalance")+"</td>");
out.println("</tr>");
if(activeTrue) {
out.println("<tr>");
out.println("<td>Currently Active</td>");
out.println("<td>Yes</td>");
out.println("</tr>");
}
else {
out.println("<tr>");
out.println("<td>Currently Active</td>");
out.println("<td>No</td>");
out.println("</tr>");
}
out.println("<tr>");
out.println("<td>Role</td>");
out.println("<td>"+ rs.getString("Role")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>BSB</td>");
out.println("<td>"+ rs.getString("BSB")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Bank Name</td>");
out.println("<td>"+ rs.getString("BankName")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Bank Account Number</td>");
out.println("<td>"+ rs.getString("AccNumber")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Superannuation Company</td>");
out.println("<td>"+ rs.getString("SuperCompany")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Superannuation Number</td>");
out.println("<td>"+ rs.getString("SuperNumber")+"</td>");
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}catch(Exception e)
{
e.printStackTrace();
}
out.close();
}
}
答案 0 :(得分:3)
out.println()在可读性方面有点难看 - 但与I / O时间相比,这些调用的成本会很小。
在循环中使用+进行字符串连接可能会损害您的内存占用(并且它也不是最快的方式)。考虑一下你有这条线:
out.println("<td>"+ rs.getString("FirstName") + " " + rs.getString("LastName") + "</td>");
您可以获得可读性和速度,并且通过使用format()
进行编码可以减少垃圾收集的剩余时间:
out.format ("<td>%s %s</td>", rs.getString ("FirstName"), rs.getString ("LastName"));
更多备注
每次调用servlet时都会创建并连接新的数据库连接。这将严重影响您的性能和可伸缩性。您应该考虑使用从中获取预连接Connections的连接池。您获得的性能将弥补很多String concats或println调用。
答案 1 :(得分:0)
如果你看一下Jasper JSP编译器的预编译输出(Apache Tomcat中的JSP编译器),这就是它将JSP转化为的内容。提高效率的唯一有效策略是考虑在服务器中传输表的数据,并在浏览器中将其转换为带有JavaScript的表。我将这种方法用于运送超过10,000行的系统(我也使用分页来保持有效负载的可管理性)