Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

The Server Side

The servlet is shown in Listing 5.

Listing 5. The LicenseServlet Servlet

package com.marinilli;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * Chapter 5 - A Simple Servlet License Manager
 *
 * @author Mauro Marinilli
 * @version 1.0
 */

public class LicenseServlet extends HttpServlet {

 /**
  * Initialize global variables
  */
 public void init(ServletConfig config) throws ServletException {
  super.init(config);
 }

 /**
  * Process the HTTP Get request
  */
 public void doGet(HttpServletRequest request,
          HttpServletResponse response)
            throws ServletException, IOException {

  String id = unscramble(request.getParameter("license-id"));
  String pwd = unscramble(request.getParameter("license-pwd"));
  Boolean outcome = new Boolean(check(id, pwd));
  response.setContentType("text/html");
  response.addHeader("outcome", outcome.toString());
  PrintWriter out = response.getWriter();
  if (outcome.booleanValue()){
   out.println("Congratulations for registering your copy!"+
         " Thank You for your choice. Please visit our web site.");
  } else {
   out.println("Sorry. License Not Valid.");
   out.println("Please Try Again.");
  }
 }

 /**
  * Clean up resources
  */
 public void destroy() {
 }

 /**
  * un-scramble a string
  */
 private String unscramble(String s) {
  return s;
 }

 /**
  * check if this pair is a valid license
  */
 private boolean check(String id, String pwd) {
  //
  return (id.charAt(0)==pwd.charAt(0));
 }

}

Let's discuss Listing 5 before getting into its deployment details.

The simple client-server communication protocol requires the client to issue a GET request passing in two parameters, license-id and license-pwd. The server (that is, the LicenseServlet) checks to see if the pair (password, id) is valid, and returns the result back to the client. The resulting Web page is used by the client MIDlet to be shown directly to the user via the wireless device screen(lines 37–43 in Listing 5).

The result of the password-id validity check is appended as a header to the HTTP response (namely, "outcome" with values "true" or "false") at line 35. Also, the unscramble method relating the communication security (lines 55–57), has been kept merely idempotent for simplicity. The check method returns true when the license id matches the password input by the user (lines 22–25), simply controlling whether the first character is the same. A more realistic implementation would require a key-validation function or accessing a database or some data file, etc.

Deploying the License Servlet

Normally, during development all the application-related files are left "loose" in the WEB-INF/classes directory in the application's document root. This could be a legitimate deployment option, but often it is preferred to pack all those items in one WAR file for easier distribution.

In Tomcat, for example, all Web applications are gathered in the directory webapps in the Tomcat home directory (from here on, called <TOMCAT_HOME>). Note that with few changes, the following procedure is applicable to any other standard servlet containers.

We assume to have created the application directory (in our case, license). Within it, the directory WEB-INF contains the classes directory (for all the Java classes—in our case, only one) and the web.xml file shown in Listing 6. All other files (HTML, JSP, etc.) may be put in the application directory. In our simple case, we don't have any static content file. Then, one may zip the resulting directory tree—once completed and tested—in a WAR file to be distributed to other servlet containers that will run our LicenseServlet.

The steps necessary to package the application for deployment are the following:

  1. Copy the WAR file under the Web application root of the servlet container.

  2. When installing the WAR file, optionally, we can add a new context (in order for the servlet container to properly recognize our application) to the server.xml file (located at <TOMCAT_HOME>/conf/server.xml). This is not necessary if the application is placed in the webapps directory and uses standard defaults. We will skip this step, assuming the standard situation.

  3. Having done this, it is enough to launch the servlet container and test the servlet using an URL compliant with that described in the Listing 6.

Listing 6 The web.xml Web Application Deployment Descriptor File

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

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

<web-app>
  <servlet>
    <servlet-name>
      license1
    </servlet-name>
    <servlet-class>
      com.marinilli.LicenseServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>
      license1
    </servlet-name>
    <url-pattern>
      /midp/*
    </url-pattern>
  </servlet-mapping>
</web-app>

(Refer to Listing 4 for details on the corresponding client for this server application).

  • + Share This
  • 🔖 Save To Your Account