Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

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.

Go to
HitServlet


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

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91630284
  • Price:- $60

Priced at Now at $60, Verified Solution

Have any Question?


Related Questions in Programming Language

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

Extend the adworks applicationi add dialogs to allow the

Extend the AdWorks application I. Add Dialogs to allow the user to Add, Edit, Read and Delete a Customer and refresh the view accordingly. 1. The user should be able to select a specific customer from the DataGrid and cl ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As