Programming | network
Introduction
J2ME (Java 2 Micro Edition) is an integral part of Java 2 and is a highly optimized Java operating environment for consumer electronic devices such as cellular telephones, videophone, digital set-top boxes, and car navigation systems. The J2ME is a Java-specific version tailored for consumer electronics and handheld devices.
The advent of J2ME has made it possible to develop applications for Cross-platform consumer electronic products. Platform-Independent features of the Java language are ported to small electronic devices to allow shared applications between mobile wireless devices. It provides an advanced Internet protocol based on HTTP that enables mobile phones to access all the information directly to the Internet in a client/server manner, allowing different client access to different resources.
With the development of network technology, in the future wireless communication era, a large number of network applications will be developed to meet the requirements of wireless mobile communication, but to fully play the wireless mobile communication equipment communication capabilities, J2ME network programming becomes particularly important.
first, use Tomcat to build a Web server
Fig. 1 Network Application Model
The vast majority of network applications are to save data to DB server (database servers), through the Web server coordination of various client requests and responses, access to data in the database. The database server and Web server may be 1 or more devices on the network, and clients can access the server side through a common Internet protocol, such as an HTTP protocol, or a Web browser on a PC, or a mobile device, A protocol that supports network traffic should be able to get the server-side resources right.
Let's start with the Tomcat Web server, where Tomcat 5 is installed. 0 first to download before
installation files for J2SDK, Tomcat 5.0
Figure 2 The installation files to prepare
1th Step installation J2SDK into the Welcome screen
Figure 3 J2SE Installation Interface 1
Step 2nd Accept the terms of the agreement and select Next
Figure 4 J2SE Installation Interface 2
The 3rd step chooses the installation path, defaults to c:\j2sdk1.4.2_05, here we do not change, choose Next, (this path is very important, if we change, we must remember).
Figure 5 J2SE Installation Interface 3
4th system will automatically copy files
Figure 6 J2SE Installation Interface 4
Step 5th installation completes click Finish.
Figure 7 J2SE Installation Interface 5
Figure 8 J2SE The directory structure after installation
Step 6th to install Tomcat 5.0, first enter the Welcome interface, click Next.
Figure 9 Tomcat Installation Interface 1
Step 7th Accept the terms of the agreement and select Next
Figure Tomcat Installation Interface 2
Step 8th choose to fully install full and then select Next
Figure One Tomcat Installation Interface 3
Step 9th to determine the installation directory, where we identified as C:tomcat 5.0, then select Next
Figure Tomcat Installation Interface 4
Step 10th determine the user name and password of the Web server administrator and the default port, and then select Next
User name Admin
Password 123 (remember that the password is used for various management of the server)
Default port 8080
Figure Tomcat Installation Interface 5
The 11th step is to determine the path to the Java virtual machine, and then select Next
Path C:\j2sdk1.4.2_05 (consistent with the path we have previously installed JDK)
Figure Tomcat Installation Interface 6
The 12th Step system automatically copies files.
Figure Tomcat Installation Interface 7
The 13th step installation completes, the system prompts whether to run immediately, click Finish, we will see the Tomcat 5.0 small icon in the lower right corner of the screen.
Figure Tomcat Installation Interface 7
The 14th step is to double-click the Tomcat 5.0 icon in the lower right corner of the screen and the Tomcat run window will pop up, click the Start service in this window to complete the Tomcat 5.0 installation, and you can enter http://127.0.0.1:8080 in the Web browser A welcome screen was detected
Figure Tomcat Installation Interface 8
Figure Tomcat Welcome Screen
After Setup completes Tomcat, the next task is to share the folders on the disk as virtual directories on the Internet for client access. We are in D: Create the following folder structure:
D:mygameweb (this directory will be used as an external virtual directory to store the various resources shared)
Figure 19 Virtual directory structure
In the browser, click the third Tomcat Manager on the top left of the page, which is a window that will pop up. Require us to enter Tomcat Admin username and password, enter username Admin password 123, press OK to enter the Web server Administration page.
Figure 20 User Management Login window
Figure The Tomcat Management window
In the admin interface, we enter the following information in the Deploy panel:
Context Path (optional):/515game (name of the virtual directory)
XML Configuration file URL:
WAR or Directory Url:file:d:/mygameweb (true folder attention format and capitalization don't be wrong)
When complete, click Deploy to publish the virtual directory
We can put the resources under the D:mygameweb directory.
Http://IP Address: 8080/515game/Resource Name to access resources on this server,
such as: http://127.0.0.1:8080/515game/myweb.html
Http://127.0.0.1:8080/515game/back0.png
Http://127.0.0.1:8080/515game/kk.wav
If this host in the public network IP address for 221.196.125.11 in the Internet can be visited the following address:
Http://221.196.125.11:8080/515game/myweb.html
Http://221.196.125.11:8080/515game/back0.png
Http://221.196.125.11:8080/515game/kk.wav
The J2ME provides access to a network of related classes, encapsulated in the Javax.microedition.io to provide the relevant classes, the following we introduce through an example if through GCF to let J2ME clients get the Web server satin resources.
Ii. getting text information from the Web
Import javax.microedition.midlet.*;
Import javax.microedition.io.*;
Import java.io.*;
Import javax.microedition.lcdui.*;
public class Gethttp
Extends MIDlet {
public void startApp () {
try {
Open Network Connection
String url = "http://127.0.0.1/515game/myweb.html";
Streamconnection sc = (streamconnection) connector.open (URL);
Reading data
InputStream is = Sc.openinputstream ();
int tmp = 0;
String get = "";
while (TMP!=-1) {//-1 represents end
TMP = Is.read ();
get = Get + (char) TMP;
}
Is.close ();
Form f = new form (URL);
Solve Chinese problems
String Chinese = new string (Get.getbytes ("iso8859-1"), "Utf-8");
F.append (Chinese);
Display.getdisplay (This). Setcurrent (f);
Turn off network connections
Sc.close ();
}
catch (Exception e) {}
}
public void Pauseapp () {}
public void Destroyapp (Boolean f) {}
}
third, get picture information from the Web
Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;
Import javax.microedition.io.*;
Import java.io.*;
public class Testpic
Extends MIDlet {
public void startApp () {
try {
Open Network Connection
String url = "Http://127.0.0.1/515game/back0.png";
Streamconnection sc = (streamconnection) connector.open (URL);
Get Pictures
InputStream is = Sc.openinputstream ();
Image im = Image.createimage (is);//The method is MIDP 2.0 method
Form f = new form (URL);
F.append (IM);
Display.getdisplay (This). Setcurrent (f);
Close connection
Sc.close ();
}
catch (Exception e) {}
}
public void Pauseapp () {}
public void Destroyapp (Boolean f) {}
}
Iv. getting multimedia information from the Web
Import javax.microedition.lcdui.*;
Import javax.microedition.midlet.*;
Import javax.microedition.io.*;
Import java.io.*;
Import javax.microedition.media.*;
public class Getmusic
Extends MIDlet {
public void startApp () {
try {
Open Network Connection
String url = "Http://127.0.0.1/515game/kk.wav";
Streamconnection sc = (streamconnection) connector.open (URL);
Get sound
InputStream is = Sc.openinputstream ();
Player P1 = Manager.createplayer (IS, "audio/x-wav");
P1.start ();
Turn off network connections
Sc.close ();
System.out.println ("Sound is play");
}
catch (Exception e) {
E.printstacktrace ();
}
}
public void Pauseapp () {}
public void Destroyapp (Boolean f) {}
}
Implementation of user Login system based on HTTP
Server-side Programs
checkuser.jsp this file under the d:/mygameweb directory.
<%
Get user Name
String Name=request.getparameter ("username");
Get the password
String pass=request.getparameter ("password");
if (Name.equals ("LDH"))
{
if (Pass.equals ("Zhm"))
{
Out.print ("Welcome");
}
Else
{
Out.print ("Pass word error");
}
}
Else
{
Out.print ("User name error");
}
%>
Client programs
Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;
Import javax.microedition.io.*;
Import java.io.*;
public class Logoin
Extends MIDlet
Implements Commandlistener, Runnable {
Public Form F;
public TextField user; User name
public TextField Pass; Password
Public Command C1;
Public Logoin () {
f = new Form ("Legendary World User Login");
user = new TextField ("username", "", Textfield.any);
pass = new TextField ("Password", "", 8, Textfield.password);
F.append (user);
F.append (pass);
C1 = new Command ("OK", command.screen, 1);
F.addcommand (C1);
F.setcommandlistener (this);
Display.getdisplay (This). Setcurrent (f);
}
public void StartApp () {}
public void Pauseapp () {}
public void Destroyapp (Boolean f) {}
public void Commandaction (Command C, displayable dd) {
Thread t = new thread (this);
T.start (); Start Thread Link Network
}
Completing network requests
public void Run () {
try {
Open Network
String url = "Http://127.0.0.1/515game/checkuser.jsp?username=" +
User.getstring () + "&password=" + pass.getstring ();
Get Data
Streamconnection sc = (streamconnection) connector.open (URL);
InputStream is = Sc.openinputstream ();
int tmp = 0;
String get = "";
while (TMP = Is.read ())!=-1) {
get = Get + (char) TMP;
}
form F2 = new form ("Landing result");
F2.append (get);
Display.getdisplay (This). Setcurrent (F2);
Turn off the network
Sc.close ();
}
catch (Exception e) {}
}
}
六、一个 Network Game Example
Below we use a network guessing price game example to show how to exchange data with the server side through J2ME.
This is an example of a network version of the product quiz, the client input commodity prices, in the server-side responsible for the game logic processing.
Server-side code:
guess.jsp this file under the d:/mygameweb directory.
<%
String Sjg=request.getparameter ("JG");
int jg= (int) (Math.random () *10);
int Userjg=integer.parseint (SJG);
if (USERJG>JG)
{
Out.println ("Sorry da le");
}
if (USERJG<JG)
{
Out.println ("Sorry Xiao le");
}
if (USERJG==JG)
{
Out.println ("right");
}
%>
J2ME Client code:
Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;
Import javax.microedition.io.*;
Import java.io.*;
public class Guessgame
Extends MIDlet
Implements Commandlistener, Runnable {
Public Form F, F2;
Public TextField TF1;
public Display D;
Public Command C1, C2;
Public Guessgame () {
f = new Form ("Commodity Quiz");
F2 = new Form ("quiz result");
C2 = new Command ("Return", Command.screen, 1);
F2.addcommand (C2);
F2.setcommandlistener (this);
TF1 = new TextField ("Please enter commodity price 1-9", "", 1, textfield.numeric);
F.append (TF1);
C1 = new Command ("OK", command.screen, 1);
F.addcommand (C1);
F.setcommandlistener (this);
D = Display.getdisplay (this);
}
public void startApp () {
D.setcurrent (f);
}
public void Pauseapp () {}
public void Destroyapp (Boolean f) {}
public void Commandaction (Command C, displayable D) {
if (c = = C1) {//Start network request
(New Thread (this)). Start ();
}
if (c = = C2) {
This.d.setcurrent (f);
}
}
public void Run () {
try {
Open Network Connection
String url = "http://127.0.0.1/515game/Guess.jsp?jg=" + tf1.getstring ();
Streamconnection sc = (streamconnection) connector.open (URL);
GET request Results
InputStream is = Sc.openinputstream ();
int tmp = 0;
String get = "";
while (TMP = Is.read ())!=-1) {
get = Get + (char) (TMP);
}
for (int i = 0; i < f2.size (); i++) {
F2.delete (i);
}
F2.append (get);
D.setcurrent (F2);
Turn off network connections
Sc.close ();
}
catch (Exception e) {}
}
}