关于delphi调用servlet的问题,请求帮助 ( 积分: 300 )

  • 主题发起人 主题发起人 阿韬
  • 开始时间 开始时间

阿韬

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=&quot;1.0&quot;?><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 = &quot;&quot;;
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(&quot;异常&quot; + e.getMessage());
}

}
 
我想用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=&quot;1.0&quot;?><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 = &quot;&quot;;
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(&quot;异常&quot; + e.getMessage());
}

}
 
这个后面客户端把前面客户端数据取走的问题我已经解决,是sevlet写的有问题,我把m_cliWriter 定义为类变更了,所以有问题。

还是想请问大家关于这种做法对性能的影响情况,请大家介绍点经验吧。[:(]
 
大家给点经验嘛
 
没有人愿意要这300分阿
 
后退
顶部