Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to read POST data in HttpServlet

807580Jan 11 2010 — edited Jan 13 2010
I'm developing a web app that will be used to query a database that contains imagery filenames and the four corners in geographic space.

I am using GlassFish v3 and have written an extension of HttpServlet to respond to queries using the doGet() method and am trying to get the ability to add entries to the database by allowing multiple images to added in a single transaction using doPost(). Unfortunately, I am having trouble trying to figure out how to get the data out of the POST!

I am using Javascript to build the POST data from the client-side (I've modified a element from Stickman that creates a way to insert multiple files into a clean easy to understand form input). I've modified the original Javascript so that the image filenames are placed into options in a select tag that allows for multiple selections.

My page is working as I want, but I am not able to retrieve the filenames in my servlet.

Here's what I have:
adminAddImages.html
<html>
<head>
   <script type="text/javascript" src="multifile.js">
</head>

<body>
   <input id="my_file_element" type="file" name="file_1">

   Files:
   <form action="http://localhost:8080/ImageSearch" method="post">
      <select id="files_list" multiple="multiple" size="1">
      </select>

      <input type="submit" value="Submit">
   </form>

   <script type="text/javascript">
      var multi_selector = new MultiSelector (document.getElementById ('files_list'));
      multi_selector.addElement (document.getElementById ('my_file_element'));
   </script>
</body>
</html>
multfile.js
function MultiSelector (list_target, max)
{
   this.list_target = list_target;
   this.count = 0;
   this.id = 0;

   if (max)
   {
      this.max = max;
   }
   else
   {
      this.max = -1;
   }

   this.addElement = function (element)
   {
      if (element.tagName == 'INPUT' && element.type == 'file')
      {
         element.name = 'file_' + this.id++;
         element.multi_selector = this;

         element.onchange = function ()
         {
            var new_element = document.createElement ('input');
            new_element.type = 'file';

            this.parentNode.insertBefore (new_element, this);
            this.multi_selector.addElement (new_element);
            this.multi_selector.addListRow (this);

            this.style.position = 'absolute';
            this.style.left = '-1000px';
         };

         if (this.max != -1 && this.count >= max)
         {
            element.disabled = true;
         }

         this.count++;

         this.current_element = element;
      }
      else
      {
         alert ('Error: not a file input element');
      }
   };

   this.addListRow = function (element)
   {
      var list = document.getElementById ('files_list');
      var fname = new Option (element.value, element.name, fale, true);

      list.options[list.options.length] = fname;
      list.size = list.options.length;
   };
}
Snippet from ImageSearch.java
for now, I am only trying to read in from the request.getReader() and writing back to the browser with the PrintWriter to see what I get. As it stands now, nothing is getting written to the browser.
public class ImageSearch
   extends HttpServlet
{
<snip...>
   public void doPost (HttpServletRequest request, HttpServletResponse response)
   {
      PrintWriter writer = response.getWriter();
      BufferedReader reader = request.getReader();

      String line;
      while ((line = reader.readLine()) != null)
      {
         writer.println (line);
      }

      writer.close();
   }
}
Should I be using something other than the getReader() to pull the data from the POST?

Any ideas will be greatly appreciated. I've spent way too much time trying to figure this out on my own and am getting desperate.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2010
Added on Jan 11 2010
15 comments
272 views