MazeDisplay.jsp Sample

This topic inludes the source code for the MazeDisplay.jsp Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebApp/handlingData/traditional_vs_pageFlow_webApp/traditional_webApp/

Sample Source Code


001 <%@ page language="java" contentType="text/html;charset=UTF-8"%>
002 <%@ page errorPage="errorpage.jsp" %>
003 <% request.setAttribute("sourcePage", request.getRequestURI()); %>
004 
005 <%@ page import="java.text.*" %>
006 <%@ page import="weblogic.jws.proxies.*" %>
007 
008 <html>
009 <body>
010 
011 <h1>Traditional JSP Version: mazeDisplay.jsp</h1>
012 
013 <% MazeGenerator_Impl proxy = new MazeGenerator_Impl("http://localhost:7001/WebServices/proxy/mazegen/MazeGenerator.jws?WSDL"); %>
014 <% MazeGeneratorSoap soapProxy = proxy.getMazeGeneratorSoap(); %>
015 
016 <% /*
017     * If the user invokes printRandomMaze, we just call soapProxy.printRandomMaze
018     * and pass the output directly to the output HTML.
019     */
020    if (request.getParameter("printRandomMaze"!= null){ %>
021      <p>You invoked the <b>MazeGenerator</b> web service's <b>printRandomMaze</b>
022      method. This is the response:</p>
023      <pre>
024 
025 <%      int rows, cols;
026         try {
027            rows = Integer.parseInt(request.getParameter("rows"));
028         }
029         catch (NumberFormatException ex) {
030            String msg = "Number of rows must be a valid integer (entered value: '" +
031                         request.getParameter("rows""')";
032            throw(new NumberFormatException(msg));
033         }
034         try {
035            cols = Integer.parseInt(request.getParameter("columns"));
036         }
037         catch (NumberFormatException ex) {
038            String msg = "Number of columns must be a valid integer (entered value: '" +
039                         request.getParameter("columns""')";
040            throw(new NumberFormatException(msg));
041         }
042 
043         /*
044          * Output the return value of printRandomMaze within a <pre> tag.
045          */
046         out.print(soapProxy.printRandomMaze(rows, cols));
047    %>
048    </pre>
049 
050 <% /*
051     * If the user invokes getRandomMaze, call soapProxy.getRandomMaze and then convert
052     * the output into images that draw the maze.  For each cell of the maze, there are
053     * 16 possible configurations of walls.  The configuration is encoded in the value
054     * returned from getRandomMaze for that cell.  The encoding is defined in
055     * MazeGenerator.jws; each cell is a 4 bit field with a bit each for left, top, right
056     * and bottom walls.
057     
058     * Prepared images for each cell configuration are stored in the images directory.
059     */
060     if (request.getParameter("getRandomMaze"!= null) { %>
061     You invoked the <b>MazeGenerator</b> web service's <b>getRandomMaze</b> method.
062     This is the response:
063     <br>
064     <% int r, c;
065        int rows, cols;
066        try {
067            rows = Integer.parseInt(request.getParameter("rows"));
068        }
069        catch (NumberFormatException ex) {
070            String msg = "Number of rows must be a valid integer (entered value: '" +
071                         request.getParameter("rows""')";
072            throw(new NumberFormatException(msg));
073        }
074        try {
075            cols = Integer.parseInt(request.getParameter("columns"));
076        }
077        catch (NumberFormatException ex) {
078            String msg = "Number of columns must be a valid integer (entered value: '" +
079                         request.getParameter("columns""')";
080            throw(new NumberFormatException(msg));
081        }
082  
083        /*
084         * call getRandomMaze and get back an array of ints containing the configuration
085         * encoding for each cell in the maze.
086         */
087        int [] rooms = soapProxy.getRandomMaze(rows,cols);
088        DecimalFormat formatter = new DecimalFormat("00");
089 
090        out.println();
091 
092        /*
093         * Loop over the cells, specifying the appropriate image for each maze cell based in
094         * the cell encoding.  The maze returned from getRandomMaze contains the bottom row
095         * first, but * we must output the top row first - so invert the rows loop.
096         */
097        for(r = rows-1; r >=0; r--)
098        {
099          for(c = 0; c < cols; c++)
100          {
101            String image_name = new String("stone_raw_" + formatter.format(rooms[r * cols + c]) +
102                                           ".gif");
103            out.print("<img src=\"/WebApp/resources/images/mazeImages/" + image_name + "\">");
104          }
105          out.print("<br>");
106          out.println();
107        }
108     %>
109 
110     <hr>
111     <p><a href="specifyMaze.html">Re-run the traditional version</a>
112     <p><a href="/WebApp/handlingData/traditional_vs_pageFlow_webApp/traditional_vs_pageFlow_webAppController.jpf">
113         Back to Traditional JSP Applications Vs. Page Flow Applications Sample</a>
114 </body>
115 </html>