Designing a View Components : Part-4

In the third part of designing a view component, we had taken overview of points like web container architecture, Now in this post we will take actually focus on writing code for class and doing further configuration.

The ListLeaguesServlet Class:

In this, it describes an implementation of the ListLeagues view component. This servlet responds to an HTTP GET request method. Therefore, it must override the doGet method.

  1. package view;
  2. import javax.servlet.http.HttpServlet;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. // support classes
  6. import;
  7. import javax.servlet.http.PrintWriter;
  8. // Model classes
  9. League;
  10. import java.util.List;
  11. import java.util.LinkedList;
  12. import java.util.Iterator;

The ListLeagues component must display a list of League objects. In this, the code to generate that list is hard coded.

  1. public class ListLeaguesServlet extends HttpServlet
  2. {
  3. private List leagueList = null;
  4. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
  5. {
  6. // create the set of leagues
  7. leagueList = new LinkedList();
  8. add(new League(2003, “Spring”, “Soccer League(Spring ‘03)”));
  9. add(new League(2003, “Summer”, “Summer Soccer Fest 2003)”));
  10. add(new League(2003, “Fall”, “Fall Soccer League 2003”));
  11. add(new League(2004, “Spring”, “Soccer League(Spring ‘04)”));
  12. add(new League(2004, “Summer”, “The Summer of Soccer Love 2004”));
  13. add(new League(2004, “Fall”, “Fall Soccer League 2004”));

Note: The code given above adds new League objects into a LinkedList. This is done in the doGet method. This means that every request resets the list to null, then adds (or re adds) the same League objects to the list.

You must set the content type to match the MIME type of the content being generated. In this example, HTML is being generated by the servlet. The bulk of code in this part is println statements that generate the HTML.


  1. // set page title
  2. String pageTitle = “Duke’s Soccer League : List Leagues”;
  3. // Specify the content type is HTML
  4. setContentType(“text/html”);
  5. PrintWriter out = response.getWriter();
  6. // generate the HTML response
  7. println(“<html>”);
  8. println(“<head>”);
  9. println(“<title>” + pageTitle + “</title>”);
  10. println(“</head>”);
  11. println(“<body bgcolor = ‘white’>”);
  12. // generate page heading
  13. println(“<!—Page Heading –>”);
  14. println(“<table border = ‘1’ cellpadding = ‘5’ cellspacing =’0’ width = ‘400’>”);
  15. println(“<tr bgcolor = ‘#CCCCFF’ align = ‘center’ valign = ‘center’ height =20>”);

Finally, the dynamic list for league title is generated. Because the list of league is represented by a List object, the loop uses an Iterator object.

  1. println(“<td><h3>” + pageTitle + “</h3></td>”);
  2. println(“</tr>”);
  3. println(“</table>”);
  4. // generate main body
  5. println(“<p>”);
  6. println(“The set of Soccer Leagues are:.”);
  7. println(“</p>”);
  8. println(“<ul>”);
  9. Iterator items = leagueList.iterator();
  10. while(items.hasNext())
  11. {
  12. League league = (League);
  13. println(“<li>” + league.getTitle() + “</li>”);
  14. }
  15. println(“</ul>”);
  16. println(“</body>”);
  17. println(“</html>”);
  18. } end of doGet method
  19. }

Design a View Component Part-4


Configuring a Servlet Definition:

  • The web.xml deployment descriptor is used by the web container for configuring your servlet components. For example, the ListLeague servlet component is declared.
  • The servlet definition is given the name ListLeagues. It is a good convention to name your servlet after the boundary component that it implements.
  • The servlet definition also specifies the fully qualified class that implements that component.
  • The web container creates an instance of each servlet definition in the deployment descriptor. You can have any number of servlet definitions.

Configuring a Servlet Mapping:

  • The deployment descriptor is also used to configure a URL pattern that is used to invoke the servletcomponent.
  • The web container does receive an HTTP requests for specific URL’s, and it must match the URL to either a physical component or a logical mapping to a servlet component.
  • For example, the web container does return the Home (index.html)page for the URL http://localhost:8080/soccer/index.html.
  • The web container does invoke the ListLeagues servlet, by calling the service method, for the URL: http://localhost:8080/soccer/list_leagues.view.

Complete Deployment Descriptor:

  • You have seen pieces of the web.xml deployment descriptor. Now in this, you are shown the complete file.
  • The first few lines of a deployment descriptor are boiler plate: the XML declaration tag (Line 1) and the web-app start tag (Line2).
  • Line 3 through 5 does declare the ListLeagues servlet definition. Line 7 through 10 does declare the ListLeagues servlet mapping.

Complete Soccer League Deployment Descriptor:

  1. <?xml version = “1.0” encoding = “ISO-8859-1”?>
  2. <web-app
  3. xmlns =
  4. xmlns:xsi =
  5. xsi:schemaLocation = version = “2.4”>
  6. <display-name>WebApp Example</display-name>
  7. <description>
  8. This web application does demonstrate a single view servlet.
  9. </description>
  10. <servlet>
  11. <servlet-name>ListLeagues</servlet-name>
  12. <servlet-class>view.ListLeaguesServlet</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>ListLeagues</servlet-name>
  16. <url-pattern>/list_leagues.view</url-pattern>
  17. </servlet-mapping>

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Reply