-1

I have the same Servlet running in two different servers. Both servers are Linux Red Hat.

This Servlet is called by some shell scripts which receives the Servlet response and uses it to make some decisions.

The problem is: Shell Scripts are very sensitive to the different kind of break lines (LF or CRLF). In one server the output of the Servlet comes with CRLF, whereas the output of the Servlet running in the other server comes only with LF. Look:

Server 1:

    0  \r  \n
 31 0d 0a

Server 2:

 0  \n
  30  0a

This is part of the Servlet code that prints the result:

Integer authorization = checkAuthorization(error_code, sat);

if(authorization != null){
    out.println(authorization);
} 

As you can see, I receive an Integer from a function and then I print it, only that.

What could justify those different outputs in the different servers? Is there any environment variable that controls that?

  • 1
    there is a concept of a platform dependant line separator. see http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character but this usually covers the difference between Unix and Windows or Mac. And this affects print writer type classes which specifically have println() methods. But a servlet doesn't have to to use these. It comes down to how your servlets are creating the output. ie. loading data from existing files. / using a JSON /XML library. is the code outputing the CR LF values implicitly? – slipperyseal Apr 19 '17 at 23:34
  • but you did mention println in your answer so check out the platform dependant line separator property – slipperyseal Apr 19 '17 at 23:38
  • Are you just looking for why this is happening or do you want a way to modify your code to ensure it doesn't happen (including cross platform)? – Alex Apr 20 '17 at 03:27
  • @slipperyseal It could be possible, per your comment I checked and noticed that one server uses java 8 on its Tomcat, the other server uses java5, maybe it is due the different JVM. I will check this property as soon as a get some time. – vinicius.olifer Apr 20 '17 at 14:35
  • @Alex For now I'm trying only to understand, since I do not feel comfortable when I don't understand things. As a way to overcome this issue, I have been using 'dos2unix' command to remove the CR. However the problem is I don't have 'dos2unix' in all my servers, so I'm afraid it could bring me problems in the future. Unfortunately, I can't install new binaries on the servers – vinicius.olifer Apr 20 '17 at 14:37

1 Answers1

1

I assume out in your code example is the writer returned by ServletResponse.getWriter() which is a PrintWriter.

Unless the servlet container tinkers with the writer implementation, it should use the platform dependent line separator which is returned by System.getProperty("line.separator"). On a unix system by default this is \n.

To solve your problem you could first check the value of System.getProperty("line.separator") on your compromised system and - in case it is the windows separator - look for code which changes the system property or check if the command which starts the server overrides the system property via a -D parameter.

Community
  • 1
  • 1
wero
  • 32,544
  • 3
  • 59
  • 84