Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

passing parameter from jsp to servlet..contd

rameshjspnetDec 3 2007 — edited Dec 3 2007
Hi all,
I amtrying to execute "fileupload" using jsp that gives input parameters to a servlet. The servlet does the operation of "fileupload". File upload is working fine but I am not able to pass nay parameter from jsp to servlet.

Say I have a textbox named as "dest"

when I call
String newfilepath = request.getParameter("dest");
in the servlet it doesn't get any value.

please go through my jsp and my servlet below and also it would help me if someone assures that I have done a right job with the following code.

JSP Code
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.util.*"%>


<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>

<!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>JSP Page</title>
           </head>
    <body><form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="text" name="dest">
<input type="file" name="filepath">
<input type="submit">


<%


Enumeration attNames=request.getAttributeNames();
String fieldName="";
String value="";
while(attNames.hasMoreElements()){
	fieldName=(String)attNames.nextElement();
	value=(String)request.getAttribute(fieldName);
}

if(value.equals("success")){
out.println("Upload Success");
}
else{

out.println("Upload failure");

}


%>
    <%--
    This example uses JSTL, uncomment the taglib directive above.
    To test, display the page like this: index.jsp?sayHello=true&name=Murphy
    --%>
    <%--
    <c:if test="${param.sayHello}">
        <!-- Let's welcome the user ${param.name} -->
        Hello ${param.name}!
    </c:if>
    --%>
    </form>
    </body>
</html>
Servlet Code
package servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import java.io.*;
import java.io.File.*;
import java.util.*;
import java.util.List;

public class UploadServlet extends HttpServlet{





private String getFileExt(String xPath)
        {  
  
			//Find extension  
			int dotindex = 0;	//extension character position  
			dotindex = xPath.lastIndexOf('.');  
			  
			if (dotindex == -1){	// no extension	  
				return "";  
			}  
			  
			int slashindex = 0;	//seperator character position  
			slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));  
			  
			if (slashindex == -1){	// no seperator characters in string  
				return xPath.substring(dotindex);  
			}  
			  
			if (dotindex < slashindex){	//check last "." character is not before last seperator  
				return "";  
			}  
			return xPath.substring(dotindex);  
	}  

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

{


	boolean isMultipart = ServletFileUpload.isMultipartContent(request);//since FileUpload.isMultiPartContent is deprecated as it has ben defined twice in that class the method has been deprecated. So use the isMultipartContent method in the ServletFileUpload class.
	if(isMultipart)
	{
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		try{
		String FileExt = ""; 
		String newFilePath="C:/Documents and Settings/Jency Madhumitha/Desktop";//request.getParameter("dest")!=null?request.getParameter("dest"):"";
		String requestedFilePath = request.getParameter("filepath")!=null?request.getParameter("filepath"):"";
		File requestFile = new File(requestedFilePath);
			
		String newFile_Name = requestFile.getName();

		FileItemFactory itemfactory = new DiskFileItemFactory();
		ServletFileUpload fileupload = new ServletFileUpload(itemfactory);

		List /* FileItem */  parseUpload = fileupload.parseRequest(request);

		Iterator uploaditr = parseUpload.iterator();

		while(uploaditr.hasNext())
		{

			FileItem item = (FileItem) uploaditr.next();
			String name = item.getFieldName();
			
			if(item.isFormField()){
			
			//out.println("getFieldName1:"+name);


			}

			else{

			String fieldName = item.getFieldName();  
			String fileName = item.getName();  
			
			File getFileName=new File(fileName);
			String newFileName=getFileName.getName();

			System.out.println("newFilePath:"+newFilePath);
			System.out.println("newFileName:"+newFileName);
			
			File uploadedFile = new File(newFilePath+"/", newFileName);  
			
			try
                                {  
					item.write(uploadedFile);  
				}  
				catch (java.lang.Exception e) {  
                                  System.out.println(e);
					
				}  

				
			if(uploadedFile.exists())
			{
				request.setAttribute("Fileupload","success");

			}
			else{
			
				request.setAttribute("Fileupload","failure");
//check for zero bytes and no filename and send failure message
				
			}
		

			
			}
			
		}
		}catch(FileUploadException er){

		System.out.println("FileUploadException:"+er);

		}
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request,response);

	}







}





}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2007
Added on Dec 3 2007
1 comment
630 views