- "); Enumeration e = v.elements(); while(e.hasMoreElements()) { String str = (String) e.nextElement(); out.print("
- "); out.println(str); } out.println("
- List all games
- List away wins
There haven't been any such games yet But the season is young; come back again soon Results Home Team Away Team Home Team Score Away Team Score The JSP starts with a page directive with an import attribute The scriptlet code is going to be using a java.util.Iterator This dependence on the java.util package must be specified: 362 JSP: Java Server Pages The page starts with a fairly typical set of tags, specifying a bean that is to be used and copying form data into the new bean (you can put the setProperty action in the body of the useBean action, or have it as a separate entity – it really makes no difference): Scriptlet code is then used to invoke the search operation: Next, there is the large conditional construct: if (theLeague.numGames()==0) { } else { } The then clause in this conditional involves simple output of fixed HTML tags and content text The else clause is more elaborate; here the table of results must be formatted This involves some template text and the iterative loop that generates rows for the HTML table The embedded scriptlet coding is not too overwhelming But even in this simple case there are constructs like: Servlet, bean and JSP examples 363 The first scriptlet here is closing the while block; then there is a fragment of conditionally included template text (the tag); and finally, a second scriptlet fragment closes the block opened in the else clause above Such code is obviously fragile A non-programming web designer who is improving this page is quite likely to move or remove one of those scriptlet tags, resulting in code that will not compile Really, the application involves a request for a search that results in one of two different responses If there are results that match the search query, e.g the client requested ‘away wins’ and there have been some away wins, their details should be listed in a well-presented table If there are no results, a different response should be generated This is basically the approach taken in the next version of this web application A pre-processing servlet is used to handle the initial request It creates and runs the SoccerSearchBean The servlet then transfers control to one or other of two different JSPs to generate appropriate responses The components in the revised version are: G Soccer.html The links now reference the servlet’s URL, as specified in a web.xml deployment file MatchReport.jsp, NoResult.jsp These separate JSP components present the different styles of response for the different search outcomes G G web.xml A web.xml deployment file is required in any more sophisticated JSP system – either one using specialized libraries, or as here working with a servlet G PreprocessServlet.java This servlet has the control logic that runs the request and forwards results to the appropriate JSP display component (Code goes in the WEB-INF/classes directory.) G SoccerGame.java , SoccerSearchBean.java and DBInfo.java These helper classes are unchanged; they are in the WEB-INF/classes/soccer directory The deployment file, web.xml, is once again a simple one It defines the servlet and the URL that will be used to reference it in the Soccer.html page: SoccerServlet PreprocessServlet ... AcmeCompany < /web- resource-name> /Rates GET POST < /web- resource-collection>... time (j) In tom/demo /WEB- INF create a file web. xml with content such as: