Skip to content

Instantly share code, notes, and snippets.

@ksoumi
Created August 1, 2019 04:46
Show Gist options
  • Save ksoumi/84b8183d7636efcd95d44dc296c400e8 to your computer and use it in GitHub Desktop.
Save ksoumi/84b8183d7636efcd95d44dc296c400e8 to your computer and use it in GitHub Desktop.
DisplayUser Servlet
package io.pivotal.workshop.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import io.pivotal.workshop.service.UserService;
import io.pivotal.workshop.service.UserServiceImpl;
/**
* Servlet implementation class DisplayUsers
*/
public class DisplayUsers extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DisplayUsers() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
UserService userService = new UserServiceImpl();
List<io.pivotal.workshop.model.User> userList = userService.retrieveAllUsers();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>WELCOME</title>");
out.println("</head>");
out.println("<body bgcolor='white'>");
//Generate page heading
out.println("<!--Page heading -->");
out.println("<table border='1' cellpadding='5' cellspacing='0' width='400'>");
out.println("<tr bgcolor='#CCCCFF' align = 'center' valign = 'center' height = '20'>");
out.println("<td><h3>List of User</h3></td>");
out.println("</tr>");
out.println("</table>");
//generate main body
out.println("<ul>");
Iterator items = userList.iterator();
out.println("<table border='1' cellpadding='5'>");
while (items.hasNext()) {
out.println("<tr>");
io.pivotal.workshop.model.User user = (io.pivotal.workshop.model.User) items.next();
out.println(user.getName());
out.println("</tr>");
}
out.println("</table>");
out.println("</ul>");
out.println("</body>");
out.println("</html>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment