问题
My code is from server jsp accessing another jsp which is in clients machine and this is perfectly communicates between client machines and server with in my office network without any issue. But the same code when my server jsp is in outside office(another network) and client machines in my network, am getting connection timeout exception in server logs. And when I connect to client machine through ajax call, i am able to get response form client.my code is below.
Java code in server jsp:
URL jspUrl = new URL("...../Test.jsp");
URLConnection servletConnection = jspUrl.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);
servletConnection.setUseCaches(true);
servletConnection.setDefaultUseCaches(true);
servletConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
OutputStream outputStream = servletConnection.getOutputStream();
ObjectOutputStream outputToServlet = new ObjectOutputStream(outputStream);
outputToServlet.writeObject(object);
outputToServlet.flush();
InputStream inputStream = servletConnection.getInputStream();
ObjectInputStream outputFromServlet = new ObjectInputStream(inputStream);
readObject = outputFromServlet.readObject();
outputFromServlet.close();
outputToServlet.close();
Ajax code in server jsp which is working:
function callAjax
{
if(window.XMLHttpRequest)
{
reqObj=new XMLHttpRequest();
}
else
{
reqObj=new ActiveXObject("Microsoft.XMLHTTP");
}
reqObj.onreadystatechange=processfunction;
reqObj.open("POST","./jspName.jsp?"+Id,false);
reqObj.send(null);
}
function processfunction()
{
try
{
if(reqObj.readyState==4)
{
if(reqObj.status == 200)
{
var responseString = reqObj.responseText;
}
}
}
catch(e)
{
//alert(e);
}
}
回答1:
First of all, make sure you are connected to Internet. Then change connection time to zero i.e infinte:
servletConnection.setConnectTimeout(0);
//Set read time out to zero if required
servletConnection.setReadTimeout(0);
来源:https://stackoverflow.com/questions/36723057/connection-time-out-while-accessing-jsp-using-urlconnection