Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Build database-powered mobile applications on the Java platform

Use JavaServer Pages as a gateway between MIDP applications and databases

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 4 of 6

Connection conn = DriverManager.getConnection("jdbc:mysql://computer/DB", DBusername, passwd);


Then, the server can send response data back to the MIDP client through the output stream. In a JSP, any text outside the scriptlet (embedded Java code) sections automatically writes to the output stream.

Session management

Since our server application needs to support simultaneous multiple users, we must track each user's state information to store notes with the appropriate usernames. However, HTTP is a stateless protocol, and each HTTP connection conducts a separate transaction with no knowledge of the context. A standard way to solve this problem is to have the server group relevant connections into sessions and store persistent state information with the sessions. Every MIDlet and JSP in our example application uses the technique discussed in this section to manage sessions.

The most common way to use sessions in HTTP applications is to embed unique identification text called cookies in HTTP headers. If the cookie from a request header matches a cookie on the server side, the server associates the current connection with a session through that cookie. On the server side, each session is an HttpSession object. The cookie handling is automatic in JSP containers.

The code below retrieves the HttpSession object for the current connection. If the current connection is not associated with any active session (i.e., if there is no valid cookie), a new HttpSession object will be created, and a new cookie associated with it will be sent back to the client for future identification.

HttpSession sess = request.getSession(true);


We can bind an arbitrary object containing persistent session-state data to a session with a unique name:

sess.setAttribute("Username", username);


We can then retrieve the bound object:

username = (String) sess.getAttribute("Username");


But for the method getSession() to get the correct HttpSession object, the MIDP client must remember to send the correct cookies. The small-footprint implementation of the MIDP HttpConnection class lacks built-in cookie support. However, HttpConnection does provide several methods to manage HTTP headers directly.

Before a MIDP client sends out an HTTP request to a server, it uses the following method to set a cookie in the request header:

conn.setRequestProperty("cookie", cookie);


After the server responds, it retrieves the kth key/value pair in the HTTP response header:

String key = conn.getHeaderFieldKey(k);
String value = conn.getHeaderField(k);


If the key equals the string set-cookie, then the value is a new cookie from the server and is stored for future use.

Automatic cookie handling

Since all communication methods in the MIDP application need to use cookies, we should encapsulate cookie-handling code in connection classes and reuse them throughout the application. Sun's new J2ME/J2EE wireless showcase Smart Ticket Demo implements a simple cookie-aware wrapper class called SessionConnector. The usage is simple: we just call SessionConnector.open() instead of Connector.open() to get new HttpConnection objects, and cookies are handled automatically. We use the SessionConnector class to track sessions in the MIDP application in our example

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources