|
JSP App
Table of Contents
This section takes you through the creation and deployment of a simple JSP application, Game of Eight. In the sample application, Game of Eight a jumbled magic square with numbers from 1 to 8 is ordered using an HTML client. The program checks for the right order every time a tile is moved. When all the tiles are ordered, a congratulatory message, along with the time taken to solve the game, is displayed. The walkthrough demonstrates the following:
In this sample, a Windows platform is assumed. To work on any other platform, follow the required file conventions on that platform. The components used in this sample are:
Creating a Directory Structure
- /src/gameofeights - /im - /WEB-INF/classes Adding Files to your Directories
<html>
<head>
<title>Game of Eight : The JSP version</title></head>
<body bgcolor=#ffffff>
<font face="Verdana, Sans-serif" size=5 color="#ff3333">
<b>Eight puzzle: Using Jsp</b>
</font>
<hr size=1 color="#000099">
<font face="Verdana, Sans-serif" size=2>
<p>
<%@ page import="gameofeights.*" %>
<jsp:useBean id="game" class="gameofeights.Eight" scope="session" />
<% String startagain = request.getParameter("startagain");
if (startagain!=null)
if (startagain.equals("true"))
game.initialize();
String message = null;
String rowVal=request.getParameter("rowVal");
String colVal=request.getParameter("colVal");
if (rowVal!=null) {
message=game.moveTile(rowVal,colVal);
}
String[][] gifarray=game.getArray();
boolean gamestatus =true;
if ( (message!=null) && (message.equals("won")) ) {
gamestatus=false;
%>
<p>Congratulations ! You won<br>
<% }%>
<table width=150 cellspacing=0 cellpadding=1 border=3 bgcolor=#ffffcc>
<% for (int i=0;i<3;i++) {%>
<tr>
<% for (int j=0;j<3;j++) { %>
<td>
<% if (gamestatus) { %>
<a href="gameofeight.jsp?rowVal=<%=i%>&colVal=<%=j%>">
<% } %>
<img src="im/<%=gifarray[i][j]%>" border=0></a>
</td>
<% } %>
</tr>
<% } %>
</table>
<% if (!gamestatus) {%>
<br><br>
<a href="gameofeight.jsp?startagain=true">Start Again?</a>
<% } %>
</p>
<hr size=1 color="#000099">
</div>
<font face="verdana, sans-serif" size=1>
<b>Aim:</b> Move the tiles to rearrange the numbers in sequence:<br><br>
1 2 3<br>
4 5 6<br>
7 8 <br><br>
<b>Moving the tiles:</b> A tile is moveable if it is adjacent to the empty square.<br>
To move a tile, click on it. The tile will move into the empty square.<br>
Clicking on a non-moveable tile will do nothing.
</body>
</html>
package gameofeights;
import java.util.Vector;
public class Eight extends Object
{
private int array[][];
private int solution[][];
private String gifArray[][];
private int row, col;
private boolean moved;
public Eight()
{
array = new int[3][3];
solution = new int[3][3];
gifArray = new String[3][3];
moved = true;
initArray();
}
public String moveTile( String rowVal, String colVal )
{
/* Time implementation
if( count == 0 )
startTime=System.currentTimeMillis();
*/
int numRow, numCol;
try
{
numRow = Integer.parseInt(rowVal);
numCol = Integer.parseInt(colVal);
}
catch( NumberFormatException exc )
{
return exc.toString();
}
if( isMoveable(array[numRow][numCol]) )
{
swap(array[numRow][numCol]);
moved = true;
//count++;
if( won() )
{
//timeTaken = System.currentTimeMillis()-startTime;
return new String("won");
}
return new String("moved");
}
moved = false;
return new String("not moved");
}
/*
public String getTime()
{
return new String(""+timeTaken/1000);
}
*/
public String[][] getArray()
{
if(!moved)
return gifArray;
for(int i=0; i<3; i++)
for( int j=0; j<3; j++ )
gifArray[i][j] = new String( array[i][j] +".gif");
return gifArray;
}
/*
public int getMoveCount()
{
return count;
}
*/
private boolean won()
{
for(int i=0; i<3; i++)
for( int j=0; j<3; j++ )
if(array[i][j] != solution[i][j])
return false;
return true;
}
private void initArray()
{
solution[0][0]=1;
solution[0][1]=2;
solution[0][2]=3;
solution[1][0]=4;
solution[1][1]=5;
solution[1][2]=6;
solution[2][0]=7;
solution[2][1]=8;
solution[2][2]=0;
// to be implemented: Random generation
array[0][0]=3;
array[0][1]=1;
array[0][2]=7;
array[1][0]=2;
array[1][1]=5;
array[1][2]=4;
array[2][0]=8;
array[2][1]=6;
array[2][2]=0;
}
private boolean isMoveable( int num )
{
row = getRow(num);
col = getCol(num);
return ( ( row-1 >= 0 && array[row-1][col] == 0 ) ||
( row+1 < 3 && array[row+1][col] == 0 ) ||
( col-1 >=0 && array[row][col-1] == 0 ) ||
( col+1 < 3 && array[row][col+1] == 0 )
);
}
private int getRow( int num )
{
for(int i=0; i<3; i++)
for( int j=0; j<3; j++ )
if( array[i][j] == num )
return i;
return -1;
}
private int getCol( int num )
{
for(int i=0; i<3; i++)
for( int j=0; j<3; j++ )
if( array[i][j] == num )
return j;
return -1;
}
private void swap( int num )
{
int blankrow = getRow(0);
int blankcol = getCol(0);
array[blankrow][blankcol] = num;
array[row][col] = 0;
}
public void initialize()
{
array[0][0]=3;
array[0][1]=1;
array[0][2]=7;
array[1][0]=2;
array[1][1]=5;
array[1][2]=4;
array[2][0]=8;
array[2][1]=6;
array[2][2]=0;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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">
<icon>
<small-icon />
<large-icon />
</icon>
<display-name>GameofEightWeb</display-name>
<description>No Description</description>
<servlet>
<servlet-name>gameofeight.jsp</servlet-name>
<display-name>gameofeight.jsp</display-name>
<description>No Description</description>
<jsp-file>gameofeight.jsp</jsp-file>
</servlet>
<session-config>
<session-timeout>-1</session-timeout>
</session-config>
</web-app>
Compiling the Java File
javac -d WEB-INF/classes src/gameofeights/Eight.java
Deploying the Application
cd c:\Dekoh\server\bin
deploy c:\GameofEight
list *in the command prompt, and check whether GameofEight is displayed in the list that comes up. Note: If you have the Dekoh Desktop already running, you must shut it down before you use the command, dekoh_shell.bat to restart the Server. Running the JSP Application
Debugging Your ApplicationThis chapter discusses how to debug the JSP application you just built using IDEs such as IntelliJIDEA and Eclipse SDK. In the following sections, we use the Debugger to:
Setting the Java Debug Option
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7777to the command, java -server -cp %CLASSPATH% %COMPILER_OPTIONS% %DATEPARSING% %REDIRECTSTREAMS% com.pramati.web.lwt.LWWebServer - nodedir ../nodes/default -httpport 8181 %NOSHELL% Debug Using Eclipse
cd c:\Dekoh\server\bin
deploy c:\GameofEight
for(int i=0; i<3; i++)inside the getRow method.
Debug Using IntelliJIDEA
cd c:\Dekoh\server\bin
deploy c:\GameofEight
for(int i=0; i<3; i++)inside the getRow method.
Distributing your ApplicationNow that you have your application running, you may want to distribute it to end users who have installed Dekoh Desktop, and will like to work with your application.To make your application distributable to the end user:
Using the Compiled JSP Pages
Compiling the JSP Application Using ANT
LIB_DIR = C:\\Dekoh\\server\\lib
pramati.lwt.jar = ${LIB_DIR}/pramati/pramati_web_lwt.jar
ant.class.path = ${pramati.lwt.jar}
app.path=C:/GameOfEight
SERVLET_ROOT = ${app.path}
web.xml.file = ${app.path}/WEB-INF/web.xml
exclude.jsp.list = \"\"
includes.jsp.list =
project.class.path = ${ant.class.path}:\
${SERVLET_ROOT}/WEB_INF/classes:\
app.path=C:/GameOfEightto point to your GameofEight root directory.
<?xml version="1.0" encoding="UTF-8"?>
<project name="AntShell" default="instrument">
<property file="das-jsp-compile.props"/>
<taskdef classname="com.pramati.tools.ant.AntTaskHandlerJSPCompiler" name="jspc">
<classpath id="jspc.classpath">
<pathelement path="${ant.class.path}"/>
</classpath>
</taskdef>
<taskdef classname="com.pramati.tools.ant.AntTaskHandlerInstrument" name="instrument">
<classpath id="instrument.classpath">
<pathelement path="${ant.class.path}"/>
</classpath>
</taskdef>
<target name="parse">
<jspc appRoot="${SERVLET_ROOT}" validateXML="true"
targetWebXmlPath="${web.xml.file}" includes="${includes.jsp.list}" excludes="${exclude.jsp.list}"
outputDir="${SERVLET_ROOT}/WEB-INF/src">
<classpath>
<pathelement path="${project.class.path}"/>
</classpath>
</jspc>
</target>
<target name="compile" depends="parse">
<mkdir dir="${SERVLET_ROOT}/WEB-INF/classes"/>
<mkdir dir="${SERVLET_ROOT}/WEB-INF/lib"/>
<javac destdir="${SERVLET_ROOT}/WEB-INF/classes" optimize="off" debug="on" failonerror="false"
srcdir="${SERVLET_ROOT}/WEB-INF/src" memoryinitialsize="256m" memorymaximumsize="512m" fork="yes">
<classpath>
<pathelement path="${project.class.path}"/>
</classpath>
</javac>
</target>
<target name="instrument" depends="compile">
<instrument srcDir="${SERVLET_ROOT}/WEB-INF/src" classDir="${SERVLET_ROOT}/WEB-INF/classes"/>
</target>
</project>
ant -f <full path to das-jsp-compile.xml>to compile the file, das-jsp-compile.xml Saving component.xml file
<?xml version="1.0" encoding="UTF-8" ?> - <component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dekoh.com/xmlns/component http://www.dekoh.com/schemas/component.xsd" xmlns="http://www.dekoh.com/xmlns/component" enabled="true"> <id>http://www.dekoh.com/ns/component/app/gameofeight</id> <version>1.0</version> <type>application</type> <display-name>GameOfEight</display-name> <description>Order a magic square jumble with numbers from 1 to It checks for the right order every time a tile is moved. When all the tiles are ordered, a success message shows up along with the time taken to solve the game.</description> </component>The version manager in Dekoh compares your version against the currently available version of the application. This will either tell you that no upgrade is needed, or that an upgrade is recommended.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:component="http://www.dekoh.com/xmlns/component"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
targetNamespace="http://www.dekoh.com/xmlns/component">
<xs:element name="callback-classes">
<xs:complexType>
<xs:sequence>
<xs:element ref="component:callback-class" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="dependencies">
<xs:complexType>
<xs:sequence>
<xs:element ref="component:dependency" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="component">
<xs:complexType>
<xs:all>
<xs:element ref="component:id"/>
<xs:element ref="component:version"/>
<xs:element ref="component:type"/>
<xs:element ref="component:display-name" minOccurs="0"/>
<xs:element ref="component:description" minOccurs="0"/>
<xs:element ref="component:updateurl" minOccurs="0"/>
<xs:element ref="component:downloadurl" minOccurs="0"/>
<xs:element ref="component:dependencies" minOccurs="0"/>
<xs:element ref="component:callback-classes" minOccurs="0"/>
</xs:all>
<xs:attribute name="enabled" use="optional">
<xs:simpleType>
<xs:restriction base="xs:boolean"/>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="version" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="dependency">
<xs:complexType>
<xs:all>
<xs:element ref="component:id"/>
<xs:element ref="component:maxversion"/>
<xs:element ref="component:minversion"/>
<xs:element ref="component:updateurl"/>
</xs:all>
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="description" type="xs:string"/>
<xs:element name="id" type="xs:string"/>
<xs:element name="maxversion" type="xs:string"/>
<xs:element name="minversion" type="xs:string"/>
<xs:element name="display-name" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="updateurl" type="xs:string"/>
<xs:element name="downloadurl" type="xs:string"/>
<xs:element name="callback-class" type="xs:string"/>
<xs:element name="version" type="xs:string"/>
</xs:schema>
Packaging the Application
jar -cfv GameOfEight.war *.jsp im WEB-INF web.xml
|