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.
- package view;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- // support classes
- import java.io.IOException;
- import javax.servlet.http.PrintWriter;
- // Model classes
- League;
- import java.util.List;
- import java.util.LinkedList;
- 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.
- public class ListLeaguesServlet extends HttpServlet
- {
- private List leagueList = null;
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
- {
- // create the set of leagues
- leagueList = new LinkedList();
- add(new League(2003, “Spring”, “Soccer League(Spring ‘03)”));
- add(new League(2003, “Summer”, “Summer Soccer Fest 2003)”));
- add(new League(2003, “Fall”, “Fall Soccer League 2003”));
- add(new League(2004, “Spring”, “Soccer League(Spring ‘04)”));
- add(new League(2004, “Summer”, “The Summer of Soccer Love 2004”));
- 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.
- // set page title
- String pageTitle = “Duke’s Soccer League : List Leagues”;
- // Specify the content type is HTML
- setContentType(“text/html”);
- PrintWriter out = response.getWriter();
- // generate the HTML response
- println(“<html>”);
- println(“<head>”);
- println(“<title>” + pageTitle + “</title>”);
- println(“</head>”);
- println(“<body bgcolor = ‘white’>”);
- // generate page heading
- println(“<!—Page Heading –>”);
- println(“<table border = ‘1’ cellpadding = ‘5’ cellspacing =’0’ width = ‘400’>”);
- 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.
- println(“<td><h3>” + pageTitle + “</h3></td>”);
- println(“</tr>”);
- println(“</table>”);
- // generate main body
- println(“<p>”);
- println(“The set of Soccer Leagues are:.”);
- println(“</p>”);
- println(“<ul>”);
- Iterator items = leagueList.iterator();
- while(items.hasNext())
- {
- League league = (League) items.next();
- println(“<li>” + league.getTitle() + “</li>”);
- }
- println(“</ul>”);
- println(“</body>”);
- println(“</html>”);
- } end of doGet method
- }
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:
- <?xml version = “1.0” encoding = “ISO-8859-1”?>
- <web-app
- xmlns = http://java.sun.com/xml/ns/j2ee
- xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
- xsi:schemaLocation = http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd version = “2.4”>
- <display-name>WebApp Example</display-name>
- <description>
- This web application does demonstrate a single view servlet.
- </description>
- <servlet>
- <servlet-name>ListLeagues</servlet-name>
- <servlet-class>view.ListLeaguesServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ListLeagues</servlet-name>
- <url-pattern>/list_leagues.view</url-pattern>
- </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.