<?xml version='1.0' encoding='utf-8' ?> <http-request> <requestType> [type of request] </requestType> <request> [Application specific request.This will be an XML Elment ] </request> </http-request> |
<?xml version='1.0' encoding='utf-8' ?> <response> <responseMessage> [the response Message] </responseCode> <responseCode> [an application specific return code.] </responseCode> <response> [Application specific request.This will be an XML Element] </response> </http-response> |
<sql-statement> [The SQL statement to be executed] </ sql-statement > 响应结果是这样的: <result-set> </result-count> [the number of rows in the result set] </result-count> <row> <col name='name'> [the value] </col> ? </row> ? <result-set> |
String className = PACKAGE_NAME + "." + request.getRequestType() + "Handler"; HttpServiceHandler handler = Class.fromName(className).newInstance(); handler.handleRequest(request); |
Public void handleRequest(ServiceRequest request) { Serviceresponse response = processRequest(request); SendResponse(response); } |
HttpServiceClient client = new HttpServiceClient(serviceURL); DBServiceRequest request = new DBServiceRequest(); request.setSqlStatement(statement); DBServiceResponse response = new DBServiceResponse(client.executeRequest(request)); |
http://myHost/servlet/httpservice.HttpService. |
POST /servlet/ httpService.Httpservice HTTP/1.0 Host: localhostt:80 Content-Type: text/xml Content-Length: 248 <?xml version='1.0' encoding='utf-8' ?> <http-request> <requestType>DBService</requestType> <request> <sql-statement> SELECT * FROM MyTable </sql-statement > </request> </http-request> |
HTTP/1.0 200 OK Date: Fri, 24 Nov 2000 16:09:57 GMT Status: 200 Servlet-Engine: Tomcat Web Server/3.1 (JSP 1.1; Servlet 2.2; Java 1.3.0; Windows 2000 5.0 x86; java.vendor=Sun Microsystems Inc.) Content-Type: text/xml Content-Length: 726 Content-Language: en <?xml version='1.0' encoding='utf-8' ?> |
public class HttpServiceClient { private static final String HTTP_VERSION = "1.0"; private String serviceUrl; public HttpServiceClient(String serviceUrl) public ServiceResponse executeRequest(ServiceRequest request) try //check for failures InputSource source = String theResponse = return serviceResponse; }
// No port? use default port 80 Socket soket = new Socket (url.getHost (), port); in = new BufferedReader (new InputStreamReader(soket.getInputStream ())); out.print (HTTP_POST_REQUEST + " " + url.getFile() + "HTTP/" + HTTP_VERSION + "\r\n"); try // I will ignore all the headers and keep reading //what remains of the input Stream is my payload } private void parseStatusLine(String statusLine) // this is the HTTP Version returnCode = Integer.parseInt (st.nextToken ()); StringBuffer retMessage = new StringBuffer (); while (st.hasMoreTokens ()) returnMessage = retMessage.toString (); } } |
Document dom; |
public class DBServiceHandler extends { String sql = request.getSqlStatement(); try } String theSql = sql.trim().toUpperCase(); try if(theSql.startsWith("SELECT")) Vector theResults = parseResultSet(resultSet); }catch(SQLException ex) response.setResponseCode(200); String res = response.serializeResponseToString("utf-8"); return response; |
public class ServiceRequest { protected Document dom; public ServiceRequest(Document request) public ServiceRequest() //initializes an empty request Element eRequestType = root.appendChild(eRequestType); Element eRequest = public String getRequestType() } public void setRequestType(String requestType) } public Node getRequest() return request.getFirstChild().cloneNode(true); } public Element createElementNode(String elementName) public Text createTextNode(String value) public void setRequest(Node request) try if(oldRequest != null) requestElement.appendChild(request); public byte[] serializeRequestToByteArray(String encoding) public String serializeRequestToString(String encoding) private ByteArrayOutputStream serializeDOM(String encoding) return bytes; protected String getTextAttribute(String name) protected void setTextAttribute(String name, String value) } public class DBServiceRequest extends ServiceRequest public final static String SERVICE_NAME = "DBService"; public final static String SQL_STATEMENT_TAG_NAME = "sql-statement"; public DBServiceRequest() public DBServiceRequest(Document request) public DBServiceRequest(ServiceRequest request) public void setSqlStatement(String sql) public String getSqlStatement() try eDBRequest.appendChild(dom.createTextNode("")); try |