How to send binary data in a form field via Java
I apologize in advance if this is not the correct mail list for such a question, but this is the closest one I could find. (I am using Tomcat 6.0.26 but my question is generic in nature) I have a Java client that talks to a servlet using several text fields. I now wanted to add a field that includes binary data (in this case a protocol buffer byte array). The data gets to the servlet, but the bytes are changed. Specifically, it appears that the encoding/decoding of bytes 127 are not the same as the original bytes. The client specified content-type to be application/x-www-form-urlencoded right now, but I have tried several others. Then, for encoding, I have tried encoding the bytes using URLEncoder ByteArrayOutputStream osBytes = new ByteArrayOutputStream(); byte[] val = ... // My binary data String valStr = new String((byte[]) val); osBytes.write(URLEncoder.encode(valStr, UTF-8).getBytes()); DataOutputStream os = new DataOutputStream(urlConn.getOutputStream()); os.write(osBytes.toByteArray()); Now, on the servlet side, I do a request.getParameter() to get the field. It is already decoded (I assume URL decoding) by the servlet container. Do I have control over the decoding? A portion of the byte array at the client is as follows... 31 12 e 8 ac e2 cb 8c 90 26 10 90 There are several variables, that I need to get right at the same time. 1) content-type... what is the correct content type 2) encoder... is URLEncoder the correct encoder? 3) encode string... UTF-8 and US-ASCII do not work. 4) do I have control over the decoding on the servlet side? 5) anything else I need to worry about? Any help would be greatly appreciated. - Tosh
Two way long running servlet connection problem
I apologize if this question has been asked before, but I could not find the question in this list. My client makes what I call long running servlet request. By that what I mean is that the client makes a servlet request and the server keeps the ServletOutputStream open for a long time. I use this connection so that the server can send periodic data down to the client. This has been working quite well. I did not need for the client to send additional data after the initial servlet request so I was closing its output stream after I made the request and the servlet did not use its corresponding ServletInputStream. Any new data from the client to the server was sent over a new short servlet request. Now I have the need for the client to send some additional data to the server, and I would really prefer to send the data over the same connection to the long running servlet. I changed my request code to keep the output stream open and send data over it. I get no error doing this. But on the server side, the long running servlet tries to read the ServletInputStream and it does not get any data. What am I doing wrong? Is this not supported? Sincerely, Tosh
Re: Two way long running servlet connection problem
Sorry about that. Here is the system description: Platform: Intel/Mac, Intel Linux, Windows 32bit. All 3 platforms have the same Tomcat/Java as below. Tomcat 6.0.26 (no web server installed) Sun Java: 1.6.0.22 Let me try to describe the code flow a bit more so that it may give a better understanding of the connection between my client and server. First of all, I did it this way because if I used a different port I could have opened a socket connection to do my bi-directional communication. Using port 80 means I have less issues about firewalls. So the flow is as follows: 1) the client worker thread make a servlet request, opens the input stream to get the servlet response. After the servlet response is received, the input stream is kept open. From now on the thread periodically checks the input stream for data and processes it. 2) On the web app side, the servlet gets the request, sends back the response, but then does not close the output stream. Instead, it does other work and periodically sends more data (binary data) on the output stream. I do not do anything special for the keep-alive. Notice that so far, this is a one-way (server down to client) flow. What I want to do is to have two-way communication over a long time. So, the modified code works like this: 1) the client worker thread makes the servlet request as usual on its output stream, but then keep that stream open, to later send additional data to the server. This data is not a servlet request but binary data of my own format. The input stream is opened to get the servlet response and to receive additional other binary data in the future. 2) On the web side, the servlet keeps open the input and output streams, sends a servlet response and then later sends binary data to the client as necessary. But now, since it has also kept open the input stream it should receive the binary data from the client... at least that was my expectation. Instead, I do not get any data (stream.available() always returns 0). I hope this makes it more clear. Based on some of the responses, I have an additional question: 1) Should I be doing something explicitly to keep the connection alive or does the connection close after some inactivity on its own? Thank you all of looking at my problem. Sincerely, - Tosh
Re: Two way long running servlet connection problem
OK. I understand. Thank you very much for your help. I will look into WebDAV and Comet. Sincerely, Tosh On Sun, Nov 21, 2010 at 3:37 PM, Mark Thomas ma...@apache.org wrote: On 21/11/2010 13:24, Paritosh Patel wrote: What am I doing wrong? You are trying to get the HTTP protocol to do something it was never intended to support. Is this not supported? Yes, this is not supported. Use Comet. Mark - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org -- Sincerely, Paritosh
How to get Tomcat IP address during app startup?
My web application needs to know the server IP/Port address during its initialization. How can I determine that? (I was trying to stay server agnostic as much as possible... if that is not possible, then I will write specific code for Tomcat. I have searched everywhere for this answer on the web and in this forum. I understand it can be found from the HttpServletRequest class, but that is too late. I have a servlet context listener, but I cannot get the IP address from there either. I would appreciate any help or ideas on this. Thanks. - Tosh