Roslyn K Sakaguchi
Roslyn K Sakaguchi

Reputation: 17

java upload servlet get additional form data

I have a java upload servlet and jsp form. the upload portion is working fine but i cannot get the regular form values in the upload servlet java code so that i can use these. Right now, I am trying to get the form data with request.getParameter("name"); but it is return null values. I am able to get the field values using item.getFieldName() and item.getFieldValue() but these are not really usable in additional processes in the item.iterator but these are not usable in further operations. For example, I want to pull in the email address field value so that I can send an email at the end of my upload servlet. I cannot use this data if I cannot make it into a string or variable which is where I am having issues. Any advice?

jsp file:

  jsp form:
     <form method="post" action="UploadServlet" enctype="multipart/form-data">
         Username: <input type="text" name="username" value="username"/><br>
         E-mail: <input type="email" name="email" autocomplete="on"><br>
         <br>
         Select file to upload:<input type="file" name="uploadFile" /><br>
         <input type="submit" value="Upload" />
     </form>

Java file:   

    UploadServlet.java

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             String id = request.getParameter("username");
             System.out.println(id); //returns null value
             String password = request.getParameter("email");
             System.out.println(password);//returns null value

Upvotes: 0

Views: 2595

Answers (1)

RDY
RDY

Reputation: 683

For using file upload you get value only in form field. request method not work in file upload

if (ServletFileUpload.isMultipartContent(request)) {                
     List<FileItem> multipart = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

      for (FileItem item : multipart) {
            if (!item.isFormField();
{
      //Your upload file code.
 }
if (item.isFormField()) { 
 
if (item.getFieldName().equals("username")) {
     String id = item.getString();
  }else if (item.getFieldName().equals("email")) {
  String password = item.getString();
        }
}

Upvotes: 1

Related Questions