Skip to main content

Posts

Showing posts with the label multipart

JEE File Upload - Handling Multipart requests in Servlets 3.0

Prior to Servlets 3.0 developers have to use 3rd party libraries to process complex multipart request that is sent when file is uploaded by user. Starting from Servlets 3.0 JEE provides a better way to handle file uploads (multipart requests) without using any 3rd party library. Making your Servlet ready to handle Multipart requests All you have to do is to add  @MultipartConfig annotation on your servlet class and it is ready to handle Multipart requests. @MultipartConfig   public class FileUploadServlet extends HttpServlet { Our example HTML form <form method="POST" enctype=" multipart/form-data "> <input type="file" name="ufile" /> <input type="submit" value="Submit" /> </form> Handling the POST in processRequest method of Servlet protected void processRequest (HttpServletRequest request,         HttpServletResponse response)         throws ServletException, IOException { Gettin...

HTTP POST request in Java

In one blog post I wrote about how we can make HTTP requests and receive response in Java. That post was only about sending GET request. This time we will learn how to make POST (submitting form etc) request in Java.  This post assumes that you are familiar with  HttpURLConnection  found in  java.net  package. If you are not please read this blog post http://random-stuff-mine.blogspot.com/2013/06/http-request-and-response-java.html HTTP POST request Lets say you entered username/password in a website and pressed "Login" button. Most probably the web browser makes a HTTP POST request containing your username/password. A simple POST request would look like this POST /path/to/script HTTP/1.1 User-Agent: RemzyHttpBot Content-Type: application/x-www-form-urlencoded Content-Length: 32   username=rameez&password=test In the above request you see that username and password are sent inside the request body and content-type of request is set to appl...