6

Servlet code

request.getparameter("fname") //I can't able to get value. 

HTML code

 <html> <head> <title>File Uploading Form</title> </head> <body> <h3>File Upload:</h3> Select a file to upload: <br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="text" name="fname" size="50" /> <input type="file" name="file" size="50" /> <input type="submit" value="Upload File" /> </form> </body> </html> 

My question is : How to pass fname parameter in multipart post request?

4
  • Then use <input type="text" name="fname" size="50" /> Type "fname" doesn't exist. See w3schools.com/tags/att_input_type.asp for aviable types. Alse in server side "name" doesn't exist either. You should use request.getparameter("fname"). Commented Dec 5, 2013 at 7:59
  • I have corrected .Still not working Commented Dec 5, 2013 at 8:09
  • Maybe action is UploadServlet.jsp? Commented Dec 5, 2013 at 8:20
  • See this post Commented Dec 5, 2013 at 8:55

2 Answers 2

5

Short answer: you will find the fname in the Parts of the request.

Long answer: For multipart type requests, even the simple <input type="text"> field values are placed in parts. You will have to iterate over the Part objects returned by HttpServletRequest.getParts() and handle them according to their name property:

for( Part p : request.getParts() ) { if( "fname".equals(p.getName()) ) { ... } else if( "file".equals(p.getName()) ) { ... } } 

To complicate things further, the content of the part is available as InputStream - Part.getInputStream() - so you will have to do a little transforming stream → byte[]String to get the value.

Sign up to request clarification or add additional context in comments.

2 Comments

request.getParts()... doesnt exist....Well, Javadocs says its available on their website, but it doesnt exist in my HttpServletRequest
I found the solution. If you also have this problem, upgrade your javax.servlet.api to 3.0.1
1
Link Each Programms ------------------------------------------------------ import java.sql.*; import java.io.*; 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.Part; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import java.util.Hashtable; import java.util.List; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.ParameterParser; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class Recent */ @WebServlet("/Recent") @MultipartConfig public class Recent extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Recent() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Sample s1=new Sample(); final String UPLOAD_DIRECTORY = "/home/pradeep/Documents/pradeep/WebContent/Images"; if(ServletFileUpload.isMultipartContent(request)){ try { List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for(FileItem item : multiparts){ if(!item.isFormField()) { String name = new File(item.getName()).getName(); item.write( new File(UPLOAD_DIRECTORY + File.separator + name)); String Path= "/home/pradeep/Documents/pradeep/WebContent/Images/" +name; s1.connecting(Path); } } request.setAttribute("message", "File Uploaded Successfully"); } catch (Exception ex) { request.setAttribute("message", "File Upload Failed due to " + ex); } }else{ request.setAttribute("message", "Sorry this Servlet only handles file upload request"); } request.getRequestDispatcher("/Result.jsp").forward(request, response); } } ------------------------------------------------------------------------------------------- import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import dbconnections.Connections; public class Sample { Connections con=new Connections(); public void connecting(String Path) { Connection conn=con.Connect(); PreparedStatement pst; String query="INSERT INTO Student1 (Path) values (?)"; try { pst=conn.prepareStatement(query); pst.setString(1,Path); pst.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub } } ------------------------------------------------------------------------------------- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div> <h3> Choose File to Upload in Server </h3> <form action="Recent" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="upload" /> </form> </div> </body> </html> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.