In this example a MIDlet accesses a simple servlet hosted on a Web server through http. The servlet returns a count of ‘hits' it has received. The MIDlet displays an interface to access the MIDlet and show the number of ‘hits'. You are required to build the system (Part i below) and then modify it (Part ii below).
Part(i) Building a Client and Server
As detailed below.
Part(ii)
Implement an extension to the system in part(i) in which several name/value pairs are passed from the server and displayed on the client interface.
For example after a "GET" the server could pass back Date, Time and Location and the client could display these on a form in separate text fields.
For part(ii) document the following:
i. Code of the programs.
ii. Output from a complete execution of the system.( screen short)
iii. Brief explanation of system behaviour with respect to all application components.
iv. Briefly discuss other types of connection that could be used to connect to the server
*****************************************************************************
Part(i) Building a Client and Server
1. Build Web Application:
File/new project: Categories=Java Web; Projects=web Application
Next >
Follow through change application name to : HitCount
No need to select any Frameworks.
2. Create a servlet Name = HitServlet
Right click on HitCount Application:
Add name; Add ClientExample package; Finish
Double click on HitServlet. Add code to servlet in editor window - See code below.
3. Add reference to index.jsp in HTML body.
4. Run the project to test the servlet.
Right click on HitCount Application > run
In the browser select the link. The servlet should return a running count of
Hits.
The servlet will also respond to access from a browser (with the above
URL) started outside NetBeans
5. Build the Midlet application. Name = HitMidlet
File>New Project>Categories=JavaME; Projects=Mobile Application>Next
Uncheck ‘Create Hello Midlet'
Accept defaults
Finish
6. Create Midlet
Name = HitMidlet
Add code. See below.
7. Create attribute in manifest files to match url of servlet.
Accessed with GetAppProperty call:
String url = getAppProperty("MIDlet-Info-URL");
On HitMidlet Right click > properties
Category > Application Descriptor >Attributes > Add
See screen below
8. Run Client.
Make sure Server is running and servlet is deployed.
Run HitMidlet.
Note outputs.
To Set Properties
Simple Server Access
MIDlet
/*
* HitMidlet.java
*/
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.*;
/**
* @author Home
*/
public class HitMidlet extends MIDlet
implements CommandListener {
private Display mDisplay;
private Form mMainForm;
private StringItem mMessageItem;
private Command mExitCommand, mConnectCommand;
public HitMidlet() {
mMainForm = new Form("HitMIDlet");
mMessageItem = new StringItem(null, "");
mExitCommand = new Command("Exit", Command.EXIT, 0);
mConnectCommand = new Command("Connect",
Command.SCREEN, 0);
mMainForm.append(mMessageItem);
mMainForm.addCommand(mExitCommand);
mMainForm.addCommand(mConnectCommand);
mMainForm.setCommandListener(this);
}
public void startApp() {
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mMainForm);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) {
if (c == mExitCommand)
notifyDestroyed();
else if (c == mConnectCommand) {
Form waitForm = new Form("Waiting...");
mDisplay.setCurrent(waitForm);
// Start new Thread to send and receive response
Thread t = new Thread() {
public void run() {
connect();
}
};
t.start();
}
}
private void connect() {
HttpConnection hc = null;
InputStream in = null;
String url = getAppProperty("MIDlet-Info-URL");
try {
hc = (HttpConnection)Connector.open(url);
in = hc.openInputStream();
int contentLength = (int)hc.getLength();
byte[] raw = new byte[contentLength];
int length = in.read(raw);
in.close();
hc.close();
// Show the response to the user.
String s = new String(raw, 0, length);
mMessageItem.setText(s);
}
catch (IOException ioe) {
mMessageItem.setText(ioe.toString());
}
// reset back to mMainForm - will display mMessageItem
mDisplay.setCurrent(mMainForm);
}
}
Servlet
// Receives HTTP GET and returns incremented counter
package ClientExample;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="HitServlet", urlPatterns={"/HitServlet"})
public class HitServlet extends HttpServlet {
private int mCount; // count number of hits
/**
* Processes requests for both HTTP GET
and POST
methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
response.setContentType("text/plain");
String message = "Hits: " + ++mCount; // increment hits
// reply
response.setContentLength(message.length());
out = response.getWriter();
out.println(message);
} finally {
out.close();
}
}
//
/**
* Handles the HTTP GET
method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
Attachment:- Logbook1_Exercise_B_actual-2.rar