luthriaajay
Programmer
How do I know whether that server outputstream is sending me a response or not?
I have opened the Client InputStream to recieve a response from a servlet,but how
can i be sure that i will receive a response from the servlet?
I cud be waiting for 15 seconds expecting a response but havent received one..
Is there any way to check whether the servlet is sending me a response?Any Method to
The reason I am asking is this.
I have written a Java Client that connects to a servlet.It has to wait for a
response from the servlet.It will wait for 5 seconds and if this doesnt recieve
a response,it will return back else it will display the response.
I have set a timer on my client for 5 seconds and a timer on the servlet for 15 seconds.
Essentially,when the client connects,the servlet response is held for 15 seconds
and the client tries for 5 seconds.
But the client is unable to exit without a response.The response comes back in 15 seconds.
The client shud have the message 'Connection Timed Out' after 5 seconds.
This means there is an error somewhere.
As the response takes 15 seconds,the client shudnt recieve one.
So,is there a way I can block the servlet response?
I am using threads and Inner classes for the timer purposes..
Please can any one help me?
ajay
Client code:
------------
<code>
public class HttpHandler {
private static String sURL="localhost";
static String sMessage="Hello Server..Client sending Data";
static DataInputStream dis = null;
static HttpURLConnection hpCon=null;
public static void main(String[] args)
{
sendData(sMessage);
}
public void TimerTest() {
NewThread nt = new NewThread();
}
public static void sendData(String sMess)
{
String response=null;
try{
// Invoke Timer
new HttpHandler.TimerTest();
URL url=null;
String uri = " + sURL + ":8080/servlet/threads.Recieve_Http_Data1";
url = new URL(uri);
hpCon=null;
hpCon = (HttpURLConnection)url.openConnection();
hpCon.setDoOutput(true);
hpCon.setDoInput(true);
// Transfer Data over http
DataOutputStream dos = new DataOutputStream(hpCon.getOutputStream());
dos.writeUTF(sMess);
}catch(IOException e)
{System.out.println("Error in Client " + e); e.printStackTrace();}
} // End of Method sendData
// Inner Class
class NewThread extends Thread
{
String response;
int i=0;
NewThread()
{
start();
}
public void run()
{
try {
while(i < 5)
{
System.out.println(i);
Thread.sleep(1000);
try {
dis = new DataInputStream(hpCon.getInputStream());
response = dis.readUTF();
// If response recieved, break off else Loop back.
if(dis !=null)
{
System.out.println("SERVER RESPONSE : " + response);
dis.close();
break;
}
}catch(IOException e){System.out.println("Here : " + e);}
i++;
} // End of While.
}catch(InterruptedException e){}
}
}
}
The Servlet
-----------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.math.*;
public class Recieve_Http_Data1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Server Ready to receive Message from application :"
System.out.println();
BufferedReader br=null;
// Data Read by the Servlet
String sMess="";
DataInputStream dis = new DataInputStream(request.getInputStream());
sMess = dis.readUTF();
System.out.println("Received from Client: " + sMess);
// Send response back after 15 seconds Only.
try {
for(int i=0;i<15;i++)
{
System.out.println(i);
DataOutputStream dos = new DataOutputStream(response.getOutputStream());
String sResponse = "Hello Client...This is server sending response";
dos.writeUTF(sResponse);
Thread.sleep(1000);
}
}catch(InterruptedException e){}
}
}
</code>
I have opened the Client InputStream to recieve a response from a servlet,but how
can i be sure that i will receive a response from the servlet?
I cud be waiting for 15 seconds expecting a response but havent received one..
Is there any way to check whether the servlet is sending me a response?Any Method to
The reason I am asking is this.
I have written a Java Client that connects to a servlet.It has to wait for a
response from the servlet.It will wait for 5 seconds and if this doesnt recieve
a response,it will return back else it will display the response.
I have set a timer on my client for 5 seconds and a timer on the servlet for 15 seconds.
Essentially,when the client connects,the servlet response is held for 15 seconds
and the client tries for 5 seconds.
But the client is unable to exit without a response.The response comes back in 15 seconds.
The client shud have the message 'Connection Timed Out' after 5 seconds.
This means there is an error somewhere.
As the response takes 15 seconds,the client shudnt recieve one.
So,is there a way I can block the servlet response?
I am using threads and Inner classes for the timer purposes..
Please can any one help me?
ajay
Client code:
------------
<code>
public class HttpHandler {
private static String sURL="localhost";
static String sMessage="Hello Server..Client sending Data";
static DataInputStream dis = null;
static HttpURLConnection hpCon=null;
public static void main(String[] args)
{
sendData(sMessage);
}
public void TimerTest() {
NewThread nt = new NewThread();
}
public static void sendData(String sMess)
{
String response=null;
try{
// Invoke Timer
new HttpHandler.TimerTest();
URL url=null;
String uri = " + sURL + ":8080/servlet/threads.Recieve_Http_Data1";
url = new URL(uri);
hpCon=null;
hpCon = (HttpURLConnection)url.openConnection();
hpCon.setDoOutput(true);
hpCon.setDoInput(true);
// Transfer Data over http
DataOutputStream dos = new DataOutputStream(hpCon.getOutputStream());
dos.writeUTF(sMess);
}catch(IOException e)
{System.out.println("Error in Client " + e); e.printStackTrace();}
} // End of Method sendData
// Inner Class
class NewThread extends Thread
{
String response;
int i=0;
NewThread()
{
start();
}
public void run()
{
try {
while(i < 5)
{
System.out.println(i);
Thread.sleep(1000);
try {
dis = new DataInputStream(hpCon.getInputStream());
response = dis.readUTF();
// If response recieved, break off else Loop back.
if(dis !=null)
{
System.out.println("SERVER RESPONSE : " + response);
dis.close();
break;
}
}catch(IOException e){System.out.println("Here : " + e);}
i++;
} // End of While.
}catch(InterruptedException e){}
}
}
}
The Servlet
-----------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.math.*;
public class Recieve_Http_Data1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Server Ready to receive Message from application :"
System.out.println();
BufferedReader br=null;
// Data Read by the Servlet
String sMess="";
DataInputStream dis = new DataInputStream(request.getInputStream());
sMess = dis.readUTF();
System.out.println("Received from Client: " + sMess);
// Send response back after 15 seconds Only.
try {
for(int i=0;i<15;i++)
{
System.out.println(i);
DataOutputStream dos = new DataOutputStream(response.getOutputStream());
String sResponse = "Hello Client...This is server sending response";
dos.writeUTF(sResponse);
Thread.sleep(1000);
}
}catch(InterruptedException e){}
}
}
</code>