阿
阿韬
Unregistered / Unconfirmed
GUEST, unregistred user!
我想用delphi写一个程序去call一个servlet,现在发现一个问题,如果一个客户端去调用这个servlet没有问题,
但是如果多个客户端同时去调用此servlet的时候,经常出现有的客户端就获取不到servlet返回的结果,
而有的客户端就把其他的客户端的返回数据也取回来了?
请问大家怎么解决?主要是有多个客户端的情况,包括多个客户端同时调用的时候的性能问题如何解决?谢谢。
delphi程序如下:
procedure TCallThread.proCallServlet;
var
Buf: Pointer;
bolResult: Boolean;
dwBufLen, dwIndex: DWord;
BytesToRead, BytesReaded: DWord;
intBeginTime: Integer;
Data: array[0..$1024] of Char;
hSession, hConnect, hRequest: hInternet;
strXML, TempStr, strResult: string;
procedure CloseHandles;
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
end;
begin
try
intBeginTime := GetCurrentTime;
strXML := '<?xml version="1.0"?><param><id>101function</id><name>大兴</name></param>';
hSession := InternetOpen(nil,
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hSession, PChar('172.18.2.50'),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hRequest := HttpOpenRequest(hConnect, 'POST', PChar('testServlet'), 'HTTP/1.0',
0, nil, INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded', 47,
PChar(strXML), Length(strXML));
dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);
bolResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,
Buf, dwBufLen, dwIndex);
if (bolResult) then
begin
while True do
begin
if not InternetReadFile(hRequest, @Data, SizeOf(Data), BytesToRead) then Break
else
if BytesToRead = 0 then Break
else
begin
TempStr := Data;
SetLength(TempStr, BytesToRead);
strResult := strResult + TempStr;
inc(BytesReaded, BytesToRead);
end;
end;
CloseHandles;
ShowMessage('Call Servlet 成功,花费时间=' + IntToStr(GetCurrentTime - intBeginTime) + ',返回=' + strResult);
end
else
begin
CloseHandles;
ShowMessage('Call Servlet 失败');
end;
except
on e: Exception do
begin
ShowMessage('发生错误,ErrMsg=' + e.Message);
end;
end;
end;
servlet主体程序如下:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doDeal(request, response);
}
protected void doDeal(HttpServletRequest request,
HttpServletResponse response) {
try {
BufferedReader curRead = null;
StringBuffer strBuf;
m_cliWriter = null;
m_cliWriter = response.getWriter();
String strRetXML = "";
String strtmp = null;
curRead = request.getReader();
strtmp = curRead.readLine();
strBuf = new StringBuffer();
while (strtmp != null) {
strBuf.append(strtmp);
strtmp = curRead.readLine();
}
//此处代码为解析前台送过来的XML包,根据XML包中的服务到数据库中去查找数据,然后再打成一个返回的XML包strRetXML
//此处需要花费一些时间
m_cliWriter.write(strRetXML);
}
catch (Exception e) {
System.out.println("异常" + e.getMessage());
}
}
但是如果多个客户端同时去调用此servlet的时候,经常出现有的客户端就获取不到servlet返回的结果,
而有的客户端就把其他的客户端的返回数据也取回来了?
请问大家怎么解决?主要是有多个客户端的情况,包括多个客户端同时调用的时候的性能问题如何解决?谢谢。
delphi程序如下:
procedure TCallThread.proCallServlet;
var
Buf: Pointer;
bolResult: Boolean;
dwBufLen, dwIndex: DWord;
BytesToRead, BytesReaded: DWord;
intBeginTime: Integer;
Data: array[0..$1024] of Char;
hSession, hConnect, hRequest: hInternet;
strXML, TempStr, strResult: string;
procedure CloseHandles;
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
end;
begin
try
intBeginTime := GetCurrentTime;
strXML := '<?xml version="1.0"?><param><id>101function</id><name>大兴</name></param>';
hSession := InternetOpen(nil,
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hSession, PChar('172.18.2.50'),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hRequest := HttpOpenRequest(hConnect, 'POST', PChar('testServlet'), 'HTTP/1.0',
0, nil, INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded', 47,
PChar(strXML), Length(strXML));
dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);
bolResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,
Buf, dwBufLen, dwIndex);
if (bolResult) then
begin
while True do
begin
if not InternetReadFile(hRequest, @Data, SizeOf(Data), BytesToRead) then Break
else
if BytesToRead = 0 then Break
else
begin
TempStr := Data;
SetLength(TempStr, BytesToRead);
strResult := strResult + TempStr;
inc(BytesReaded, BytesToRead);
end;
end;
CloseHandles;
ShowMessage('Call Servlet 成功,花费时间=' + IntToStr(GetCurrentTime - intBeginTime) + ',返回=' + strResult);
end
else
begin
CloseHandles;
ShowMessage('Call Servlet 失败');
end;
except
on e: Exception do
begin
ShowMessage('发生错误,ErrMsg=' + e.Message);
end;
end;
end;
servlet主体程序如下:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doDeal(request, response);
}
protected void doDeal(HttpServletRequest request,
HttpServletResponse response) {
try {
BufferedReader curRead = null;
StringBuffer strBuf;
m_cliWriter = null;
m_cliWriter = response.getWriter();
String strRetXML = "";
String strtmp = null;
curRead = request.getReader();
strtmp = curRead.readLine();
strBuf = new StringBuffer();
while (strtmp != null) {
strBuf.append(strtmp);
strtmp = curRead.readLine();
}
//此处代码为解析前台送过来的XML包,根据XML包中的服务到数据库中去查找数据,然后再打成一个返回的XML包strRetXML
//此处需要花费一些时间
m_cliWriter.write(strRetXML);
}
catch (Exception e) {
System.out.println("异常" + e.getMessage());
}
}