Connection time out while accessing jsp using URLConnection

雨燕双飞 提交于 2020-01-25 20:26:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!