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 7859891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:04:05+00:00 2026-06-02T22:04:05+00:00

The following HTML snippet submits the file to UploadHandler which is a servlet. Then

  • 0

The following HTML snippet submits the file to UploadHandler which is a servlet. Then there is a caption box that also needs to be handled. I can handle the caption box in UploadHandler and then open the connection with the database and submit it there. But i don’t want to do this . Let upload handler handle uploading of files. Then what is the alternative ? How do i submit the caption into the table ? I want to create a sense of parallelism in handling these two jobs.

<form method="post" action="UploadHandler" enctype="multipart/form-data">
        <table>

            <tr>
                <td> <strong> Browse photo to submit </strong> </td>
                <td> <input type="file" name="ImageToUpload" value="Upload Photo"/> </td>
            </tr>

            <tr>
                <td> <strong> Give a Caption to this photo </strong>  </td>
                <td> <input type="text" name="caption box" size="40" /></td>
            </tr>

            <tr colspan="2">
                <td> <input type="submit" value="submit photo"/> </td>
            </tr>

        </table>
    </form>

Is there any way that when i click to submit 2 different jobs they are handled by 2 different servlets ? Creating a new thread from UploadaHandler doesn’t seem a good idea.

After the comment by @Luiggi Mendoza :

Servlet that handles Uploading of files :

package projectcodes;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

public class UploadHandler extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    response.setContentType("text/plain");
    String path = request.getParameter("ImageToUpload");
    PrintWriter writer = response.getWriter();
    try {
        Boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(!isMultipart) {
            Boolean AttemptToUploadFile = true;
            RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
            request.setAttribute("UploadAttempt", AttemptToUploadFile);
            rd.forward(request, response);
        } else {
            DiskFileItemFactory diskFileItem = new DiskFileItemFactory();
            ServletFileUpload fileUpload = new ServletFileUpload(diskFileItem);
            List list = null;

            try {
                list = fileUpload.parseRequest(request);
            }catch(Exception exc) {
                Boolean AttemptToUploadFile = true;
                RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
                request.setAttribute("UploadAttempt", AttemptToUploadFile);
                rd.forward(request, response);
            }

            Iterator iterator = list.iterator();
            while(iterator.hasNext()) {
                String emailOfTheUser = null;
                FileItem fileItem = (FileItem)iterator.next();
                if(!fileItem.isFormField()) {
                    String fieldName = fileItem.getFieldName();
                    String fileName = FilenameUtils.getName(fileItem.getName());
                    HttpSession session = request.getSession();
                    if(!session.isNew()) {
                        emailOfTheUser = (String)session.getAttribute("Email");
                    }
                    File file = new File("/home/non-admin/project uploads/project users/" + emailOfTheUser ,fileName);
                    fileItem.write(file);
                    RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
                    String message = "File Uploaded successfully !";
                    request.setAttribute("SuccessMessage", message);
                    rd.forward(request, response);
                }
            }
        }
    }catch(Exception exc) {
        Boolean AttemptToUploadFile = true;
        RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
        request.setAttribute("UploadAttempt", AttemptToUploadFile);
        rd.forward(request, response);
    }
}

}

  • 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-02T22:04:07+00:00Added an answer on June 2, 2026 at 10:04 pm

    I guess you can if you forward from the first to the 2nd servlet, using:

    getServletContext().getRequestDispatcher("/2ndServlet").forward(req, res);
    

    But this is not a good idea, as it might trigger filter, the response may already by committed, etc.

    What you should do is extract the functionality of the 2nd servlet in a helper class, and just invoke it, as a simple java method invocation, from the 1st servlet.

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

Sidebar

Related Questions

The following snippet submits a caption to the database. After filling in the text
Consider the following HTML snippet. The desired effect is to have a dropdown be
I run the following html snippet in IE8 and IE7 with non-English characters (we
Consider the following HTML snippet containing some javascript utilizing prompt and unload . The
I have the following snippet in one of my html pages : <div class=inputboximage>
I'm using the following code-snippet extensively in my model templates. <div class=control-group> @Html.LabelFor(model =>
I'm having a problem with the following code snippet: <!DOCTYPE html> <html> <head> <title>Hangman</title>
From this site: http://www.toymaker.info/Games/html/vertex_shaders.html We have the following code snippet: // transformations provided by
The following HTML snippet is rendered with a whitespace between the two words (with
The following html snippet <div dir=rtl> test (test) </div> shows

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.