我正在創建一個servlet來顯示我生成的一個小程序的前端,在這個程序中我有一個LinkedList調用執行隊列,我將它放在一個字符串生成器中。html表格格式的字符串生成器
public String getJobsForPrint() {
Iterator<JobRequest> it = ExecutionQueue.iterator();
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
while (it.hasNext()) {
JobRequest temp = it.next();
result.append(this.getClass().getName()).append(" Object {").append(NEW_LINE);
result.append(" User ID: ").append(temp.getUserID());
result.append(" Start Date: ").append(temp.getStartDate());
result.append(" End Date: ").append(temp.getEndDate());
result.append(" Deadline Date: ").append(temp.getDeadDate());
result.append(" Department: ").append(temp.getDepartment());
result.append(" Project Name: ").append(temp.getProjectName());
result.append(" Project Application: ").append(temp.getProjectApplication());
result.append(" Priority: ").append(temp.getPriority());
result.append(" Cores: ").append(temp.getCores());
result.append(" Disk Space: ").append(temp.getDiskSpace());
result.append(" Analysis: ").append(temp.getAnaylsis()).append(NEW_LINE);
result.append("}");
}
return result.toString();
在我的servlet側由我打電話字符串:
protected void processExecutionQueue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Execution Queue</title>");
out.println("</head>");
out.println("<body>");
out.println("<div>");
out.println("<div style='position:absolute; top:20px; right: 20px;'><a href='/ProjectAndBackend/index.jsp'>LOGOUT</a></div>");
out.println("</div>");
out.println("<p>Queue:</p>");
out.println("Execution Queue:" + SystemServlet.getScheduler().getJobsForPrint());
out.println("</body>");
out.println("</html>");
}
所以現在我展示後相互串從鏈表所採取的所有數據。我想在網頁上,能夠將這些數據放入表格中,使其看起來更整潔,而不僅僅是將字符串扔到網頁上。
我該如何實現一個html來顯示某些方面的字符串的具體元素 因此,下面的例子中,我有頭然後數據是從字符串中取出元素,並繼續寫出來,直到所有的數據從迭代器顯示
<table border="1">
<tr>
<th>User ID</th>
<th>Start Date</th>
</tr>
<tr>
<td>User ID DATA</td>
<td>Start Date DATA</td>
</tr>
或者,如果任何人都可以直接我一個例子,因爲我無法找到任何與我目前的搜索。
你做了上面完全相同的事情。抓住你的用戶列表,迭代它並創建一個字符串'「th」+ user.getId()+「」'然後你添加到你的html字符串中。 **注**這是你能做的最糟糕的事情。閱讀:http://stackoverflow.com/tags/servlets/info – 2013-04-05 20:17:05
如果您不想在Java代碼中創建表格HTML,那麼您將需要使用諸如JSP或JSF之類的東西。如果你想用你的Java代碼來做,請執行_SotiriosDelimanolis_建議的操作。 – jahroy 2013-04-05 20:17:15