Establish a servlet and application communication through HttPurlConnection

xiaoxiao2021-03-06  36

Once once, it is necessary to implement the application to call EJB service, but consider that other applications may be different languages, first consider using COBAR, later because they are not responsible for this project, they have not continued to study, recently due to project needs, Considering the drawback of COBAR, you see some other solutions, and then collect information, and recently use servlet to implement communication with application. (Extended, you can use serveet to quit EJB's stuff).

First of all: Establish an application, limited to technical reasons, using J2SE to write a relatively simple application, the application is responsible for the data to be processed, (if it is for Java, it can be an object, using the object stream) Send to the web server The corresponding URL / servlet, after processing, returns the data of the response.

The understanding of this communication in the page is to use the HTTP protocol, by establishing an application on the servlet's httpurlconnection:

//establish connection

URL URL = New URL ("http: // localhost / datareceptiveservlet? Userid = lhz & tel = 1111111111 & type = 0 & length = 8");

UrlConnection urlconn = (urlConnection) URL.OpenConnection ();

/ / Set the connection attribute

Urlconn.setdoOutput (TRUE);

Urlconn.setusecaches (false);

UrlConn.seueStproperty ("Content-Type", "Application / Octest-Stream");

Urlconn.setrEquestProperty ("Content-Length", "" buf.length ());

/ / Generate data stream

String buf = "aaaaaaaa";

DataOutputStream DataOut = New DataOutputStream (UrlConn GetoutputStream ());

DataOut.writeutf (buf);

Dataout.flush ();

Dataout.close ();

/ / Accept the response data of the servlet

DataInputStream in = New DataInputStream (urlconn.getinputStream ());

String response = in.readutf ();

System.out.println ("Read from Server:" Response);

In.Close ();

Establish DataReceiveServlet

Public void dopost (httpservletRequest Request, httpservletResponse response) throws servletexception, ioException

{

Perform (Request, Response);

}

Public Void Perform (HttpservletRequest Request, HttpservletResponse Response) Throws ServletException, IOException

{

INT TYPE = Integer.Parseint (Request.GetParameter ("Type"). Trim ());

String userid = Request.getParameter ("userid"). Trim ();

String Tel = Request.getParameter ("tel"). Trim ();

INT TYPEINDEX = Request.getQueryString (). Trim (). Indexof ("Type"); DataInputStream IN = New DataInputStream ());

Response.setContentType ("Application / Octest-Stream");

// Type = 2

IF (Type == 2)

{

String queryString = Request.getQueryString (). Trim (). Substring (TypeIndex 7);

Action1 Action1 = New Action1 (DS);

Action1.createmms (QueryString, IN, UserID, Tel, Type);

} else if (Type == 0 || Type == 1) // SMS data or EMS

{

// Quick Action

Action Action = New Action (DS);

Action.create (In, UserID, Tel, Type);

}

/ / Return to the client

ByteArrayoutputstream byteout = new byteArrayoutputStream ();

DataOutputStream out = new dataoutputstream (byteout);

Out.writeUTF ("Accept Data");

Out.flush ();

BYTE BUF [] = byteout.tobytearray ();

Response.setContentLength; BUF.LENGTH;

ServletOutputStream servletout = response.getOutputStream ();

ServletOut.write (buf);

servletOut.close ();

In.Close ();

Return; files that can be sent are not just text

Can be a picture

,sound

And other documents

, You must generate the desired file array

Generate data streams and transfer

.

转载请注明原文地址:https://www.9cbs.com/read-66134.html

New Post(0)