1

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.

2
  • You did the exact same thing above. Grab your List of users, iterate over it and create a String "th" + user.getId() + "</th>" which you then add to your html String. Note this is the worst thing you could do. Read this: stackoverflow.com/tags/servlets/info Commented Apr 5, 2013 at 20:17
  • If you don't want to create the table HTML in your Java code, you will want to use something like JSP or JSF. If you want to do it with your Java code, do what SotiriosDelimanolis has recommended. Commented Apr 5, 2013 at 20:17

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes basically you can create the html code you want the servlet to show, inside your getJobsForPrint() method. Although it would be cleaner to modify the getJobsForPrint() so that it gives you a List of Strings instead of one single String as return. And in your servlet that calls the getJobsForPrint() you put the list entries into a html table as Joseph Selvaraj suggested. The Servlet will then be automatically transformed into a html site that you can see in the browser.
excellent, thanks for the fast reply, managed to implement it with the way you suggested Joseph, going to attempt to do the suggestion by yourself now GameDroids then I know how to implement several ways , thanks both of you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.