0

I have 1 Servlet with a doPost method which will upload an image. But I need to create a Pattern object first so I can get the ID of this pattern and use this to make this the name of the image. (Not interesting for the question but now you know my motives)

This is my Servlet called CreatePatternServlet which uses the doGet:

 public void doGet(HttpServletRequest req, 
               HttpServletResponse resp)
              throws ServletException, java.io.IOException {
       /*--------------------creating the pattern-------------------------*/
       //TODO make pattern
       String name = req.getParameter("name");
       String scope = req.getParameter("scope");
       String purpose = req.getParameter("purpose");
       String problem = req.getParameter("problem");
       String solution = req.getParameter("solution");

       System.out.println("step 1: ["+name+","+ scope+","+purpose+","+problem+","+solution+"]");

       //This method returns the id of the pattern, which will be used for the name of the image
       int patternId = Controller.createPattern(name, scope, purpose, problem, solution);

       /*------------------end of creating pattern-----------------------*/

And this will be the doPost in UploadImageServlet:

doPost(HttpServletRequest req, HttpServletRespose resp) throws ServletException, IOException{
      isMultipart = ServletFileUpload.isMultipartContent(req);

      java.io.PrintWriter out = resp.getWriter( );
      if( !isMultipart ){
         return;
      }

      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File(repository));

      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );

      try{ 
      // Parse the request to get file items.
      List fileItems = upload.parseRequest(req);

      //calling the function which loads the image in the default directory
     Controller.uploadImage(fileItems, patternId);

      }catch(Exception e){
          System.out.println("oeps");
      }
      /*--------------------ending creation of image, image is created--------------------*/

   }

Independently they work perfectly fine but I would like to combine them. The uploadImage can't run with a doGet so I will need some way to forward the Get request to the Post request. Is this possible? Or should I find a workaround?

Ivar Reukers
  • 7,560
  • 9
  • 56
  • 99
  • Just move common code to a reusable class/method which is in turn called by both methods? This is not a servlet problem. This is just basic Java. – BalusC Dec 01 '15 at 13:21
  • How do you do the upload? Is it some HTML-Form or a program? AFAIK query-parameters (?name=Value) are possible in POST URLs as well...? – Jan Dec 01 '15 at 13:21
  • okay - so you POST all those parameters in one request. So all of them will be received encoded on the server. – Jan Dec 01 '15 at 13:32

1 Answers1

0

Start off with reading and understanding this post: How to upload files to server using JSP/Servlet?

Once you did, you should be able to replace what you do in doGet() with appropriate calls in doPost in order to patch it all together.

Community
  • 1
  • 1
Jan
  • 13,738
  • 3
  • 30
  • 55