Implement an extension to the system

Assignment Help Programming Languages
Reference no: EM13962258

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.

<h1>Go to
<a href="https://localhost:8080/HitCount/HitServlet">HitServlet </a>

</h1>


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 <code>GET</code> and <code>POST</code> 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();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> 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 <code>POST</code> 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";
}// </editor-fold>

}

 


Attachment:- Logbook1_Exercise_B_actual-2.rar

Reference no: EM13962258

Questions Cloud

Overforecasting demand create within supply chain : What problems does overforecasting demand create within a supply chain? What problems does underforecasting demand create? What can a company do to resolve the problem of forecasting inaccuracy?
Probability distribution of the household size : Although the government has offered various incentives for population control, some argue that the birth rate, especially in rural India, is still too high to be sustainable. A demographer assumes the following probability distribution of the hous..
What is the daily demand of this product : Viapy is a company that produces all kinds of major appliances. Suzan Curtis, the president of Viapy, is concerned about the production policy for the company's best selling refrigerator. What is the daily demand of this product? If the company were ..
What penalty is the company incurring per year : A food processor uses approximately 100,000 glass jars a year for fruit juice product. Annual holding cost is 40 cents per jar, and reordering cost is $128 per order. Because of storage limitations, a lot size of 4,000 jars has been used. What penalt..
Implement an extension to the system : 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.
Show that after the force is removed : A particle of mass ,m, is at rest at the end of a spring(force constant=k)hanging from a fixed support. At t=0 a constant downward forcd F is applied to the mass and acts for a time T. Show that after the force is removed, the displacement of the ..
How much safety inventory does the store carry : Demand during lead time for HP printers at a Sam's Club store is normally distributed, with a mean of 500 and a standard deviation of 150. The store manager continuously monitors inventory and orders 1,000 printers when the inventory drops to 800 pri..
Find the probability that at any given time : Spoke Weaving Corporation has eight weaving machines of the same kind and of the same age. The probability is .04 that any weaving machine will break down at any time. Find the probability that at any given time
Do you think that such ordinances violate the principles : Pit bulls, a breed of dog, were involved in several attacks on humans in recent years. Do you think that such ordinances violate the principles of due process set forth in the Constitution, or do they effectively balance the interests of all those ..

Reviews

Write a Review

Programming Languages Questions & Answers

  Program to find real solutions

Method, which determines whether value of "b squared" - 4ac is negative. If negative, code then  prints out message "no real solutions" and returns from method.

  Whether a string is a palindrome or not

Please examine the attachment, this is as far as I got, however I am able to compile but when executing I get and error. Please select palindrome files to save in the pals.txt file.

  Write a loop that will show the price of silver and gold

Write a C++ program that displays a table showing the price of silver and gold. The table will have three columns. The first column will be labeled Ounces, the second labeled Silver

  Design and implement a java program

Design and implement a Java program which defines an array of size SIZE, randomly populated with Integer or int values in the range 1 .. MAXRNG and sorts the array in increasing order of its values using QuickSortOpt1 and then by QuickSortOpt2. Consi..

  Design program that asks user to enter budgeted amount

"Design a program that asks the user to enter the amount that he or she has budgeted for a month. (For example: $2,000.00)

  Function named quadratic that receives three parameters

Write the definition of a function named quadratic that receives three double parameters a , b , c . If the value of a is 0 then the function prints the message "no solution for a=0" and returns.

  Write complete payroll program for a company

Now, write a complete PAYROLL program for a company in which each employee falls into one of the 3 categories - Administrative, FactoryEmployee or Salesperson.

  Website design html

It's all about Website design. It is going to be web site. 3 web pages, 2 font styles, 2 font sizes, 2 colors minimum.

  Include hyperlink for confirming order in online ticket

Include a check box for attending the Awards Event. Include a hyperlink for confirming the order. Make the link invisible to begin but display it after the Submit button has been clicked.

  Explain drawbacks to using ajax technology

What are some of the other drawbacks to using AJAX technology? Why are some of these very significant items to consider before implementing AJAX on a given website?

  Input number by user and display positive and negative

Input a number entered by the user and display "positive" if it is greater than zero, "negative" if it is less than zero, and "zero" if it is equal to zero.

  Write program that uses recursive function to count number

Write a program that uses a recursive function to count the number of blobs in a square grid. Input to the program should consist of the locations of the asterisk in the grid.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd