Results 1 to 7 of 7
  1. #1
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default Servlet and byte array

    I have a swing application that connects to an application server via a servlet. Currently, I am using a PostMethod and adding a parameter like the following: method.addParameter("stringName", stringValue). In the servlet, I get the value from the HttpServletRequest via request.getParameter("stringName").
    The issue I am running into is that I added compression and the compression converts the string to a byte array. I cannot figure out how to send the byte array and receive it via the HttpServletRequest because the PostMethod parameter only takes a string. I have seen some examples use an entity but I am not sure exactly how to implement that. Any help would be great and thanks in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Servlet and byte array

    What packages are you using to do the http calls?

    What does the code look like that builds the request?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default Re: Servlet and byte array

    The packages:
    (CLIENT)
    org.apache.commons.httpclient.HttpClient
    org.apache.commons.httpclient.methods.PostMethod;

    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(URL);
    method.addParameter("stringName", compressedString);
    int statusCode = client.executeMethod(method);

    (SERVER)
    javax.servlet.http.HttpServlet;
    javax.servlet.http.HttpServletRequest;
    javax.servlet.http.HttpServletResponse;

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
    String stringValue = request.getParameter("stringName");
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Servlet and byte array

    Just checking.
    OK.

    So since you intend to send bytes, and you control the receiving end, you could simply Base64 encode the byte[], then decode it at the other end.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default Re: Servlet and byte array

    I guess my main problem here is how do i send the byte array? I am currently only able to send a String in the post method parameter.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Servlet and byte array

    That's what Base64 encoding is for.
    It converts arrays of bytes into a String so you can stick it in a POST block (for example).
    Here's the Java class that does all this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default Re: Servlet and byte array

    Thank you, that's exactly what I needed.

Similar Threads

  1. Bit Array to Byte Array in java
    By Umesh Joshi in forum New To Java
    Replies: 1
    Last Post: 11-30-2012, 08:24 AM
  2. Replies: 1
    Last Post: 10-21-2011, 11:55 AM
  3. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  4. String Array to a Byte Array?
    By lazarus1410 in forum Advanced Java
    Replies: 1
    Last Post: 12-03-2010, 08:25 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •