Table of ContentsA Simple Networked MIDletOnline Scoring for Star Assault

The Server Side

To add communications to your games, you also need to set up an HTTP server that will listen for (and respond to) requests you make.

Because you're dealing with standard HTTP as the transport, you don't have to use a Java-specific solution for your server system. PHP, CGI, and Perl are all common solutions for server-side development. However, there is the obvious advantage to utilizing Java in dealing with a single code base (as well as working with only one language). You'll also find J2EE to be a powerhouse of functionality when you start getting more serious.

The best HTTP server solution for your purposes is Tomcat, part of the Apache software project.

Setting Up Tomcat

You can obtain Tomcat from the Apache binaries page at http://jakarta.apache.org/site/binindex.cgi. (The home page is http://jakarta.apache.org/tomcat/index.html.) Download the server archive in a format appropriate for your system and unzip it into a directory.

To get things working, navigate to the Tomcat /bin directory and execute the startup.bat (startup.sh for UNIX) file. After the server starts up, you can make sure that everything is working properly by opening up a Web browser and going to the URL http://localhost:8080/. If everything is set up correctly, you'll see an introduction page in your browser window.

Creating a Servlet

To handle incoming requests in Tomcat, you need to create a mini program that resides inside the server. These mini programs are known as servlets, and they are built according to Sun's J2EE specifications.

To create a servlet you need to create a class that extends the abstract javax.servlet.http.HttpServlet, and then implement either the doGet or doPost method to handle incoming requests.

NOTE

Note

Note that to use javax you will need to have the servlets API to your class path. If you don't already have this you can find a version in the servlet-api.jar file under the common/lib subdirectory where you installed Tomcat.

NOTE

Note

For your purposes you can use either an HTTP GET or POST request; however, GET uses the query string part of a URL for parametersfor example, http://localhost:8080/test.jsp?query=10. POST, on the other hand, transmits request parameters as a payload embedded into the request. The only real drawback to using GET (which is a little simpler) is you need to parse and unparse requests from the query string, and those query strings are limited in length. For these reasons, I recommend using POST.

Here's an example of a servlet that accepts POST requests and returns a hello in response.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
    {
        // open up the input stream and convert it into a string
        InputStream in = req.getInputStream();
        int requestLength = req.getContentLength();
        if (requestLength > 0)
        {
            StringBuffer sb = new StringBuffer(requestLength);
            for (int i=0; i < requestLength; i++)
            {
                int c = in.read();
                if (c == -1)
                    break;
                else
                    sb.append((char)c);
            }
            in.close();
            String rs = sb.toString();
            System.out.println("Got request: " + rs);
        }

        // process things
        // do something with the request

        // send back a response
        res.setContentType("text/plain");
        PrintWriter out = res.getWriter();
        out.write( " HELLO ");
        out.close();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
    {
        doPost(req, res);
    }

}

Once you have a servlet compiled into a class file, it's ready for deployment to the server.

Deploying a Servlet

To get a servlet functioning, you need to deploy it into the server. For Tomcat, you do this by first creating a new project directory for your application in the /webapps subdirectory of the Tomcat installation directory.

The project directory needs to contain another subdirectory named WEB-INF, and below that another directory named classes.The final structure should look like this:

/Tomcat-base-directory
    /webapps
        /test
            /WEB-INF
                web.xml (see below)
                /classes
                    SimpleServlet.class

Next you need to create a Web project file called web.xml in the WEB-INF directory. The contents of this file are used to set up the project. Here's an example:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

The first part of the web-app section is to set the concise (display) name and the long text description.

    <display-name>Test</display-name>
    <description>Simple Test Application</description>

Next you need to create a reference to the servlet class. This doesn't have to be a class in the WEB-INF/classes directory. In can be anything in the Tomcat class path. The servlet-name specified here is how this servlet is identified.


<servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>SimpleServlet</servlet-class>
</servlet>

Now the servlet has been defined you can use it by "mapping" it to a URL. In this case if the server sees an incoming URL of /hello (appended to the end of the application URL) the request will be passed to the referenced servlet (Test).

    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

The main function of this example web.xml file is to set up the servlet for access. This is done first by using the <servlet> to declare the mapping between a servlet nametag and a servlet class. You then use the tag to map to a specific URL (/hello).

To make the servlet work, you first need to copy the SimpleServlet.class file into the WEB-INF/classes directory.

To access your new project, you need to restart Tomcat and then navigate to the URL corresponding to the project. In your case this is just the directory name you used under webapps (test) plus the URL you specified pointing to the servlet in the web.xml file (/hello). Therefore, the full URL is http://localhost:8080/test/hello. If everything is working correctly, the server will respond with a "Hello".

Those are the basics of setting up your server and installing a servlet. In the next section you'll bring it all together in a working example.

    Table of ContentsA Simple Networked MIDletOnline Scoring for Star Assault