1. Create your own Myservletcontextlistener.java:
Package YYBWB;
Import Java.net.ServerSocket;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
public class Myservletcontextlistener implements Servletcontextlistener
{//This enables the class to implement the Servletcontextlistener listener interface, which is invoked when Tomcat is started
The Contextinitialized method
ServerSocket SS = null;//Declaration ServerSocket Object
Serverthread st = null;//Declaration Serverthread Object
Create a service thread and start it, this method is called when TOMCA starts
public void contextinitialized (Servletcontextevent sce) {
try{ss = new ServerSocket (8888);
st = new Serverthread (ss);
St.start ();
}catch (Exception e) {e.printstacktrace ();
} }
Stop the service thread, this method is called when TOMCA stops
public void contextdestroyed (Servletcontextevent sce) {
try{St.flag = false;
Ss.close ();
SS = NULL;
st = NULL; }catch (Exception e) {e.printstacktrace ();
} } }
2. Configure the above listening class in Web. xml
<Listener>
<listener-class>xx. Myservletcontextlistener</listener-class>
</Listener>
3. Create a service thread Serverthread class such as:
Package YYBWB;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.net.SocketException;
public class Serverthread extends thread{
Public ServerSocket SS; Declaring ServerSocket objects
public Boolean flag = FALSE; Loop flag bit
Public Serverthread (ServerSocket ss) {//constructor
THIS.SS = SS;
Flag = true; }
The Run method creates a ServerSocket listener
public void Run () {
while (flag) {
try{
Socket socket = ss.accept ();
After the client connects, create the agent thread ServerAgent, which is responsible for communicating with the customer
ServerAgent sa = new serveragent (socket);
Sa.start ();
}
catch (SocketException se) {
try{Ss.close ();
SS = NULL;
System.out.println ("ServerSocket closed");
}catch (Exception ee) {
Ee.printstacktrace ();
}
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
}
4. Agent thread serveragent, such as:
Package YYBWB;
/**LASTDATE:2014-10-22 * Update Menu-#GET_MENULIST # * Author:wuchyuan * *
Import static YYBWB. constantutil.delete_success;
Import static YYBWB. constantutil.diary_success;
Import static YYBWB. Constantutil.register_fail;
Import static YYBWB. constantutil.update_state_success;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.EOFException;
Import java.io.IOException;
Import Java.net.Socket;
Import java.net.SocketException;
Import Java.sql.Blob;
Import java.util.ArrayList;
Import java.util.List;
public class ServerAgent extends thread{
public socket socket;
public DataInputStream din;
Public DataOutputStream dout;
Boolean flag = false;
Public serveragent (socket socket)
{this.socket = socket;
try {This.din = new DataInputStream (Socket.getinputstream ()); This.dout = new DataOutputStream (Socket.getoutputstream ());
Flag =true;
} catch (IOException e) {e.printstacktrace (); }}//Method: Thread Execution method
public void Run () {
while (flag) {
try {String msg = Din.readutf (); Receive messages from clients
SYSTEM.OUT.PRINTLN ("The message Received is:" +msg);
if (Msg.startswith ("< #LOGIN #>")) {//message is logged in
String content = msg.substring (9); Get message Content
String [] sa = Content.split ("\\|");
arraylist<string> result = Dbutil.checklogin (Sa[0], sa[1]);
if (Result.size () >1) {//Login successful
StringBuilder sb = new StringBuilder ();
Sb.append ("< #LOGIN_SUCCESS #>");
for (String S:result) {
Sb.append (s);
Sb.append ("|"); }
String logininfo = sb.substring (0,sb.length ()-1);
Dout.writeutf (Logininfo); Returns the user's basic information}
else{//Login failed
String Logininfo = "< #LOGIN_FAIL #>" +result.get (0);
Dout.writeutf (Logininfo); } }
MEALSVR's message, 2014-7-9
else if (Msg.startswith ("< #PRINT_DINGD #>")) {
Message print order for query 2014-7-9
msg = msg.substring (15); Extract Content
String [] sa = Msg.split ("\\|"); Split string
Boolean result = Dbutil.queryprint (sa[0]); Query whether there is a need to print orders
if (result) {//There is an order
Dout.writeutf ("< #HAVE_DINGD #>"); Send back Success Message
Dout.flush ();
} else{
Dout.writeutf ("< #NOT_DINGD #>"); Send back Create failure message
Dout.flush (); } }
2014-10-22 Check User password
else if (Msg.startswith ("< #CHECK_USERPASSWD #>")) {
msg = msg.substring (20); Extract Content
String [] sa = Msg.split ("\\|"); Split string
Boolean result = DBUTIL.CHECKUSERPW (sa[0],sa[1]); Query whether there is a need to print orders
SYSTEM.OUT.PRINTLN ("Query" +sa[0]+ "password is correct");
if (result) {//has
Dout.writeutf ("< #CHECKPWD_HAVE #>"); Send back Success Message
Dout.flush (); }
else{
Dout.writeutf ("< #CHECKPWD_NOT #>"); Send back Create failure message
Dout.flush ();
}
}
}
} catch (SocketException se) {try {
Dout.close ();
Din.close ();
Socket.close ();
socket = NULL;
Flag = false;
} catch (IOException e) {
E.printstacktrace (); }
} catch (Eofexception eof) {
try {dout.close ();
Din.close ();
Socket.close ();
socket = NULL;
Flag = false;
} catch (IOException e) {
E.printstacktrace (); }
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}
Android Server-side design