Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:08:35+00:00 2026-06-09T08:08:35+00:00

I’m using jsp and a servlet to do this. I have a contact form

  • 0

I’m using jsp and a servlet to do this.

I have a contact form that send a email with some data (name, subject, question,contact email etc) and a file.

when I submit the form, and get the servlet response only the first thing is returned.

String file= fileUpload(request); //upload the client's file and return the absolute      path of the file in the server
//testing the rest of parameters
 out.println("REQUEST LIST"
              "\n"   request.getParameter("name")
              "\n"   request.getParameter("mail")
              "\n"   request.getParameter("subject")
              "\n"   request.getParameter("ask")
              "\n");

In this order the file is uploaded succesfully, but the other parameters (name, mail etc) are null.


In the order below, the parameters are ok, they return the data correctly. But the file is not uploaded.

//testing the rest of parameters
out.println("REQUEST LIST"
              "\n"   request.getParameter("name")
              "\n"   request.getParameter("mail")
              "\n"   request.getParameter("subject")
              "\n"   request.getParameter("ask")
              "\n");
String file= fileUpload(request); //upload the client's file and return the absolute      path of the file in the server

How can I have both?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:08:37+00:00Added an answer on June 9, 2026 at 8:08 am

    Look if the following code helps you. This is just an example. You may have to tweak it

    Create a class called FileUploader which returns ServletFileUpload object

    public class FileUploader 
    {
    private static ServletFileUpload uploader;
    
    private FileUploader()
    {
    
    }
    
    public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB) 
    {
        if(uploader == null)
        {
            DiskFileItemFactory factory = new DiskFileItemFactory();
    
            factory.setSizeThreshold(1024 * 1024);
            factory.setRepository(new File(tempDir));
    
            uploader = new ServletFileUpload(factory);
    
            uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024);
        }
    
        return uploader;
    }
    }
    

    Now you can process a request and read all the data

        protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request)
    throws FileSizeLimitExceededException
    {
        if(!isMultipartRequest(request))
            return null;
    
        ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50);
        MultiPartFormData data = new MultiPartFormData();
        try
        {
            List<FileItem> items = upload.parseRequest(request);
    
            for (FileItem item : items) 
            {
                if(item.isFormField())
                {
                    data.getParameters().put(item.getFieldName(), item.getString());
                }
                else
                {
                    String filename = item.getName();
    
                    //Internet explorer and firefox will send the file name differently
                    //Internet explorer will send the entire path to the file name including 
                    //the backslash characters etc ... we should strip it down
                    //THIS IS HACKY
                    if(filename.indexOf("\\") != -1)
                    {
                        int index = filename.lastIndexOf("\\");
                        filename = filename.substring(index + 1);
                    }
    
    
                    if(filename == null || filename.equals(""))
                    {
                        //do nothing 
                    }
                    else
                    {
                        File uploadFile = new File(uploadDir + File.separator + randomFileName);
                        item.write(uploadFile);
    
                                                data.addFile(item.getFieldname(), item.getString());
                    }
                }
            }
        }
        catch(FileSizeLimitExceededException e)
        {
            throw e;
        }
        catch(Exception e)
        {
            e.printStackTrace();
    
        }
    
    
        return data;
    }
    

    After parsing the request I am storing it in some object called MultipartFormData which can be used to get request parameters

    public class MultiPartFormData {
    
    private Hashtable<String, String> parameters;
        private Hashtable<String, String> uploadedFiles;
    
    public MultiPartFormData()
    {
        this.parameters = new Hashtable<String, String>();
        this.uploadedFiles = new Hashtable<String, String>();
    }
    
    public Hashtable<String, String> getParameters() {
        return parameters;
    }
    public void setParameters(Hashtable<String, String> parameters) {
        this.parameters = parameters;
    }
        public void getParameter(String paramName) {
              if(this.parameters.contains(paramName))
                     return tyhis.parameters.get(paramName);
              return null;
        }
        public void addFile(String key, String filename) {
            uploadedFile.put(key, filename);
        }
        public void getFilename(String key) {
            uploadedFile.get(key);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.