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

  • SEARCH
  • Home
  • 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 6060803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:52:01+00:00 2026-05-23T08:52:01+00:00

I want to create a servlet that will allow me to upload image files

  • 0

I want to create a servlet that will allow me to upload image files from the client to the server. I am helping myself with the tutorial i found at apache site:
http://commons.apache.org/fileupload/using.html

On my way i am finding some complications and doubts:

Question 1

I would like my servlet to prepare an object with all the values from the request(included images as byte[]) and pass it to an @EJB that will insert all in the database.
Is that possible? Could you give some pseudo code tips on how to improve my current servlet to do that?

@WebServlet(name="uploadServlet")
public class FileUpload extends HttpServlet {

    @EJB
    private DBAccessEJB ejb;

    private static final long serialVersionUID = -1062753489906645120L;

    // Will be triggered when a post method is sent by the user(Example: Form
    // submit)
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // Check if the request is multipart
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);

           // Create the object that willBe passed to the EJB 
              Thing thing = new Thing();

        if (isMultipart) {
            // If it is multipart, save the items in a FileItemfactory
            FileItemFactory factory = new DiskFileItemFactory();
            // Create an upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            try {
                // Get the files out of the request
                List items = upload.parseRequest(req);
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) {
                    // Get each of the items inside the items list
                    FileItem item = (FileItem) iterator.next();
                    // If the item does not come from a field
                    if (!item.isFormField()) {
                       //transform the uploaded images to byte[]
                                       //setTheImageValues of the object
                    }
                                    else {
                                       //set the text values of the object
                                    }
                }
                          //Pass the prepared object to the EJB to be inserted in DB
                          ejb.save(thing)
            } catch (FileUploadException fue) {
                fue.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

Question 2

I thought about passing the request to the servlet through the managaged bean, instead from the JSF page, but i don’t really know how to do it. Could you give me some tips? I also don’t know how to do it in the normal way, from the page,what do you think would be the best way?
This is what i did so far regarding to the managed bean:

public void save() {

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletRequest req = (HttpServletRequest)fc.getExternalContext().getRequest();

//What else do i need here to pass the request to the server?
}

This would be at the page inside a multipart form:

<h:commandButton value="Submit" action="myBackingBean.save"/>

Question 3

In my JSF page i have more or less 10 values almost all are Strings. I take them from the JSF and temporary store them in the JSF page. If the servlet could take all the values from the request, there would be no need for this attributes in the backing bean. Do you think is this approach a good thing to do? Will this be process transaction secure, or is there any risk?

  • 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-05-23T08:52:01+00:00Added an answer on May 23, 2026 at 8:52 am

    Question 1-

    Absolutely you will need Unique Identifiers for your files, but that becomes less complicated if you do things like storing files in folders by date/username, etc…

    Here is a basic workflow for your program that you could use, based on what you have shown so far:

    Client computer -> FileUploadServlet (utilizing Apache Commons File Upload)

    Once inside the FileUploadServlet:

    a) Save the information from the request to a Database by way of your EJB including the file name, Mime Type, information, etc…

    b) While still inside the servlet, upload the file to your server, or if you need to, use a commercial solution such as Amazon S3 or Google Storage (by way of a Java API such as JetS3t)

    Afterwards, return any necessary information to the client.

    Question 2 –

    What is your reasoning for requesting throught the Bean, why not just make the Servlet the action instead, and collect the information from the request? I would not make the Bean’s save method available on the JSF, as that sounds insecure and un-authenticated.

    Question 3 –

    Same as above, why store information, even if temporarily, when it is available elsewhere?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a JSP page or servlet that will work in 2
I want to upload images from the client to the server. I already managed
I want to create a simple http proxy server that does some very basic
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a number of masked edit extenders from codebehind. Something like:
I've recently run into a snag that will probably keep me from being able
I want to create a Thread safe JSP page. It is possible in Servlet
I want to use a temp directory that will be unique to this build.
I created a servlet in java that will give me a xml response when

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.