0

I did this implementation to send a Post message to my servlet:

String urlParameters  = "my_file="+LargeString;
            byte[] postData       = urlParameters.getBytes(StandardCharsets.UTF_8);
            int    postDataLength = postData.length;
            String req        = "http://localhost:8080/MyServlet1";
            URL    url            = new URL( req );
            HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
            conn.setDoOutput( true );
            conn.setInstanceFollowRedirects( false );
            conn.setRequestMethod( "POST" );
            conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
            conn.setRequestProperty( "charset", "utf-8");
            conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
            conn.setUseCaches( false );
            try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
                wr.write( postData );
            }

In myServlet1 i try to retreive this data by using:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String mydata_serialized = request.getParameter("my_file");
            System.out.println(mydata_serialized);
}

Nothing happen. Even if i print a simple message such as System.out.println("Hello"); in doPost nothing happen. This is proof that the message is not sent to doPost

How can I retreive the message sent please ?

Mehdi
  • 1,960
  • 6
  • 32
  • 45

1 Answers1

-2

request.getParameter("my_file") will look on the URL itself to pick off URL params. to find the value for my_file.. not in the body of the request.

so you can maybe utilize the requestproperty and store it there..in the POST message do:

connection.setRequestProperty(key, value);

then pull it back out in the doPost()..

request.getRequestProperty(key);

or i think... if you leave your first code set as is and utilize

BufferedReader reader = request.getReader();

to pull out the body (you are putting the value of postData in the body of the request it appears.. so you must look to reading the body on the doPost() Servlet to pull the info back out.. not the URL String

DaveTheRave
  • 465
  • 2
  • 5