I have a website that as soon as it issues a POST request from my server (which returns a String), the web application on the client loses connection with the server on Safari 7 (on OSX 10.9.1 Mavericks), but works fine on Safari 5 (on Mac 10.6.8 Snow Leopard), Firefox, IE, Chrome.
Using Safari 7's Web Inspector, issuing the Post operation returns the error Failed to load resource: The network connection was lost.
Using a network analyzer, I inspected the packets going to the server and coming back. Actually, everything looked fine. And, the HTTP response was status=200 (OK). By process of elimination, I removed code from the Java POJO being called at login, and eventually narrowed down the fault to one line of code: out.flush(). I've summarized how it is used in the Java Servlet snippet of code below. Commenting out the flush() enabled Safari 7 to run fine.
I'm not sure why flush() causes a problem for Safari. This seems like a pretty general Java design pattern (that is, to flush() before close()). As far as I can tell in Java, close() is not guaranteed to include a flush() command. However, I did notice the packet size reduced slightly without the flush() included, and Safari may be closing the connection if the received number of bytes is different than the expected number of bytes. The following link suggests this...
The other browsers also work OK when I comment out the flush() code.
My questions for Java experts:
Is the design pattern of issuing a
close()without first issuing aflush()robust? Will I run into problems down the road? Why or why not?Anyone have a better theory why Safari doesn't like
flush()?
-----------Java Servlet---------
package com.mycompany.servlet;
...
public class UserLogin extends HttpServlet {
protected void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
// get input parameters
userEmail=req.getParameter("uEmail");
userPwd=req.getParameter("uPwd");
// query database to retrieve variable1, variable2, etc.
...
// write output response
PrintWriter out = res.getWriter();
out.println(variable1);
out.println(variable2);
out.println(variable3);
...
out.flush(); // this line causes Safari 7.0 on Mac OSX 10.9.1 to lose connection
out.close();
}
}