• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ENCTYPE='multipart/form-data and request.getParameter

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I have a query,

I am just testing something out and came across this problem. I have a form which has 'ENCTYPE='multipart/form-data' in its decleration.
Form contain simple input type is text with name='test' and a Submit button.

This calls process.jsp which wants to retrieve the parameter for 'test' ie - what the user has input into the text box.
Thing is using String test = request.getParameter("test"); prints NULL when form has 'ENCTYPE='multipart/form-data' if I remove this prints value input by user. I need the form to be of this ENCTYPE because I will also have input type='file' for user to browse for a file, because process.jsp will also have upload functionality.

I think there is a simple solution to this. Anyone know what?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marie McKeown:

I think there is a simple solution to this. Anyone know what?



The getParameter method doesn't work with multipart forms.
You'll need to check with the documentation for whatever upload library you're using.
I've used both the O'Reilly Multipart Request and the jakarta/commons/fileupload libs from Apache.
Both provide easy ways to retrieve form parameters.

There is more here:
http://faq.javaranch.com/view?FileUpload
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get jakarta-commons file-upload component this problem can be solved easily.File component as well as textfield dat can be handled together
refer this url:
http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html?page=last
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try --> MultipartFormDataRequest mrequest=new MultipartFormDataRequest(request);
mrequest.getParameter("txtEmpId");
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marie McKeown wrote:Hello I have a query,

I am just testing something out and came across this problem. I have a form which has 'ENCTYPE='multipart/form-data' in its decleration.
Form contain simple input type is text with name='test' and a Submit button.

This calls process.jsp which wants to retrieve the parameter for 'test' ie - what the user has input into the text box.
Thing is using String test = request.getParameter("test"); prints NULL when form has 'ENCTYPE='multipart/form-data' if I remove this prints value input by user. I need the form to be of this ENCTYPE because I will also have input type='file' for user to browse for a file, because process.jsp will also have upload functionality.

I think there is a simple solution to this. Anyone know what?



Just go to http://code.google.com/p/apache-commons-fileupload-lab/ and checkout the source code
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
use concept of formField to get file path or parameter vale.
get the FileItem type as eather parameter is i"FormField" or not "FormField"
check if it's "isFormField() " :=> if it's a formfield and you wil get the parameter value
otherwise you wil get the path to :uploading file"

following code snippet can help you: (here i have take two file uploads and one input parameter (opType); based upon input parameter value i upload first file or second file"

public void doPost(HttpServletRequest request, HttpServletResponse response)
{
System.out.println("Inside File upload");
TESTDatabaseManager databaseListener =(TESTDatabaseManager)getServletContext().getAttribute("Database");
TESTConfigurationRequestHandler configureRequestHandler = new TESTConfigurationRequestHandler();
HttpSession session = request.getSession();
String DPXLSPath = null;
String DETSXLSpath = null;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
((DiskFileItemFactory) factory).setSizeThreshold(1000);
ServletFileUpload upload = new ServletFileUpload(factory);
FileUploadListener listener = new FileUploadListener();
String operationType = null;
session.setAttribute("LISTENER", listener);
upload.setProgressListener(listener);
if(!isMultipart)
{
System.out.println("error");
}

else
{
upload.setSizeMax(1000000000);
boolean uploadStatusFlag = false;
try
{
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
String inputName = null;
String inputValue = null;
String fullFileName = null;
System.out.println("Number of Items = " + items.size());
String uploadDirPath = new TESTCommonUtility().getParameterFromConfig("TEST_repository_location");

while(iter.hasNext()){
FileItem item = (FileItem)iter.next();

if(item.isFormField()){
inputName = (String)item.getFieldName();
if(inputName.equalsIgnoreCase("opType")){
operationType = (String)item.getString();
System.out.println("OPERATION TYPE:"+operationType);
}

}else if( !item.isFormField() ) {

//int res = processUploadedFile(item, request, response, databaseListener);
String name = item.getName();
System.out.println("get name value :"+name);
if(name.equalsIgnoreCase(null)||name.equalsIgnoreCase("")){
System.out.println("NULL / BLANK VALUE FOR FILE.......!!!");
}else{
String fileName = name.substring(name.lastIndexOf("\\")+1);
System.out.println("Name of Item = " + item.getName() + " fileName = " + fileName);
fullFileName = uploadDirPath + "\\" + fileName;


System.out.println("FullFileName = " + fullFileName);
try {
item.write(new File(fullFileName));
uploadStatusFlag = true;
System.out.println("Upload file is saved as : " + fullFileName);

} catch (Exception e) {

e.printStackTrace();
uploadStatusFlag = false;
}
}

}

}

if(operationType.equalsIgnoreCase("firstFileUpload")){
{

// do first file upload operation
}
}else if(operationType.equalsIgnoreCase("secondFileUpload")){

// do second file upload operation

}
else{
System.out.println("WRONG INPUT TYPE");
}


}
catch (FileUploadException e)
{

e.printStackTrace();
}

}
System.out.println("End of File upload");

}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are not able to get file values if you are using enctype=multipart/form-data with request.getparameter() so here is a link for good example when working with multipart...may be it can help

http://webideaworld.blogspot.in/2013/04/woking-with-enctypemultipartform-data.html
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im using servlet.jar file (you can pass xml variable declaration if use this jar)

assume i created servlet AddStudent.

you must add a line of code "@MultipartConfig" in your servlet.



Example code :
-----------------


package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
* Servlet implementation class AddStudent
*/
@WebServlet("/AddStudent")
@MultipartConfig
public class AddStudent extends HttpServlet {
private static final long serialVersionUID = 1L;
     
   /**
    * @see HttpServlet#HttpServlet()
    */
   public AddStudent() {
       super();
       // TODO Auto-generated constructor stub
   }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               // TODO Auto-generated method stub
       }
}
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic