Reputation: 311
I'm creating a servlet to display a front end of a little program I've produced, In this program I have a LinkedList call Execution Queue that I place in a string builder.
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();
on my servlet side I call the string by:
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>");
}
So now I display strings after each other with all the data taken from the linkedlist. I want to on the webpage side, be able to take that data and put it into a table so that it looks neater and not just strings tossed onto the webpage.
How would I implement a html to show the specific elements of the string in certain aspects So with example below I have the headers then where the data is take the element from the string and keep writing it out until all the data from the iterator displayed
<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>
Or if anyone can direct me to an example as I can't find any with my current searches.
Upvotes: 1
Views: 21617
Reputation: 2237
When you build the StringBuilder use HTML tags to place each in a row.
Code should be something like this
StringBuilder result = new StringBuilder();
result.append("<tr><td>").append(User ID DATA).append("</td><td>").append(Start Date DATA).append("</td></tr>");
Note:
1.You need to create the base html and table header columns inside processExecutionQueue()
method.
2. Only the data row needs to be created in getJobsForPrint()
method.
So when the result is passed from getJobsForPrint()
it will be embedded into other HTML files.
Hope you can complete the code with this suggestion.
Upvotes: 4