0

I need to pass text field along with a image to a jsp page, and then in the jsp i want to retrieve the text field value. I am able to get the file in jsp and perform the required functions on it but I'm not able to get the text field value.

The, request.getParameter(); , function is returning null, and in this case, i've searched all the questions on stackoverflow, and it's not a duplicate question, plz provide a sol. :) Thank You.
This is the HTML file :

<form name="upload" action="upload.jsp" method="post" enctype="multipart/form-data"> <input type="text" name="pro_sp" placeholder="product selling price"></p> <input type="file" name="img">
<input type="submit" name="upload"></form>

This is the JSP file :

<%@page import="javax.servlet.annotation.MultipartConfig"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<% String x=request.getParameter("pro_desc");
out.print(x);
File file ;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = "F:/img/";
String fname="";
String contentType = request.getContentType();
if((contentType.indexOf("multipart/form-data") >= 0)) 
{

  DiskFileItemFactory factory = new DiskFileItemFactory();
  // maximum size that will be stored in memory
  factory.setSizeThreshold(maxMemSize);
  // Location to save data that is larger than maxMemSize.
  factory.setRepository(new File("c:\\temp"));

  // Create a new file upload handler
  ServletFileUpload upload = new ServletFileUpload(factory);
  // maximum file size to be uploaded.
  upload.setSizeMax( maxFileSize );
  try
  { 
     // Parse the request to get file items.
     List fileItems = upload.parseRequest(request);

     // Process the uploaded file items
     Iterator i = fileItems.iterator();

     out.println("<html>");
     out.println("<head>");
     out.println("<title>JSP File upload</title>");  
     out.println("</head>");
     out.println("<body>");
     while ( i.hasNext () ) 
     {

        FileItem fi = (FileItem)i.next();
        if ( !fi.isFormField () )   
        {
        // Get the uploaded file parameters
        String fieldName = fi.getFieldName();
        String fileName = fi.getName();
        boolean isInMemory = fi.isInMemory();
        long sizeInBytes = fi.getSize();

        // Write the file
        if( fileName.lastIndexOf("\\") >= 0 )
        {
        file = new File( filePath + 
        fileName.substring( fileName.lastIndexOf("\\"))) ;
        }
else
        {
        file = new File( filePath + 
        fileName.substring(fileName.lastIndexOf("\\")+1)) ;
        }
       fi.write( file ) ;
       fname=(filePath + fileName);
       out.println("Uploaded Filename: " + fname + "<br> |" );

        }

     }      
       out.println("</body>");
     out.println("</html>");
  }
  catch(Exception ex) {
     System.out.println(ex);
  }
}
else
{    
out.println("    <html>");
out.println("    <head>");
out.println("    <title> Servlet upload    </title>");
out.println("    </head>");
out.println("    <body>");
out.println("    <p>No file uploaded    </p>");
out.println("    </body>");
out.println("    </html>");
}

This is the output : null Uploaded Filename: F:/img/Chrysanthemum.jpg

Ashish Sharma
  • 663
  • 5
  • 15

1 Answers1

1

See this question : Servlet get parameter from multipart form in tomcat 7

Pure form parameters must be handled differently when using enctype="multipart/form-data". More details within the best answer of this question.

Community
  • 1
  • 1
Ephi
  • 346
  • 2
  • 12