File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to pass values from html to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to pass values from html to jsp" Watch "how to pass values from html to jsp" New topic
Author

how to pass values from html to jsp

Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

i am uploading a file to remote system
i need to send some values from html to jsp
in the html i am using the form tag as

<FORM name="loadfile" ACTION="file_upload1.jsp" ENCTYPE="multipart/form-data" METHOD="POST">

i am not able to pass values from html to jsp when
the ENCTYPE is used in the form tag
when i remove the ENCTYPE i am able to pass the values to jsp

how to solve this problem
waiting for your valuable suggestions



regards
santosh
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
For your kind information, request.getParameter() would always return null when enctype is multipart.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
http://faq.javaranch.com/view?FileUpload
Check this out.
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

thanks for the relpy
yes i am getting null value then how can i handle this
i need to send values from html which has ENCTYPE to jsp page
how can this be done


waiting for your reply

regards
santosh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Just to set a couple of things straight:

Your issue has nothing to do with the fact that your first page is HTML and your next page is a JSP.

Also, the value are getting passed just fine. But as Adeel pointed out, when the form is a multi-part form (or more precisely, when the form's encoding type is anything but x-www-form-urlencoded -- the default), request.getParameter and related methods do not work.

To get the values, the request must be parsed as a multi-part request. This is why so many people usually use one of the handy 3rd party libraries outlined in the FAQ entry that Adeel pointed you to.
[ November 09, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

i am using the apache fileupload itself
my html code is

upload.html
---------------

<html>
<head>
<title>Upload File...</title>

</head>
<body>
<FORM name="loadfile" ACTION="file_upload1.jsp" ENCTYPE="multipart/form-data" METHOD="POST">
<table width="87%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="21%"> </td>
<td width="40%">Which file do you want to upload ?......</td>
<td width="12%"><INPUT TYPE=FILE NAME=fileload></td>
<td width="27%"> </td>
<td width="0%"> </td>
</tr>
<tr>
<td>  </td>
<td>Select the Category to upload file .......</td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<select name="select_load" >
<option value="io">I/O</option>
<option value="memory">Memory</option>
<option value="testchips">Test Chips</option>
<option value="stdcell">Standard Cell</option>
<option value="general">General</option>
</select>
</font></td>
<td> </td>
<td> </td>
</tr>

<tr>
<td> </td>
<td> </td>
<td><INPUT name="SUBMIT" TYPE=SUBMIT></td>
<td> </td>
<td> </td>
</tr>
</table>
<p> </p>
<p> </p>
<p>  </p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> </font></p>
<p><BR>
</p>
</FORM>
</body>
</html>


jsp page is

file_upload1.jsp
--------------------
<%@ page import="java.util.*,java.io.*,org.apache.commons.fileupload.*" %>

<HTML>
<BODY>
<%

String cat_load="";
String loadtext="";
if(request.getParameter("select_load")!=null)
{
cat_load=(String)((request.getParameter("select_load")).trim());
}


out.print("...........1"+cat_load+"<br>");
out.print(cat_load.equalsIgnoreCase("General"));

if(cat_load.equalsIgnoreCase("general"))
{
out.println("inside if");

try {

out.print("inside try</b><br>");
// Create a new file upload handler

DiskFileUpload upload = new DiskFileUpload();

// Set upload parameters
// set memory size allowed to the uploading (optional)
// upload.setSizeThreshold(2000);
// set a directory to temporarily upload the file if
// the last memory size in insufficient (optional) :
// upload.setRepositoryPath("c:/temp");
// set the max uploaded file size (-1 for no max size)
upload.setSizeMax(-1);

// Parse the request
List items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();

while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
// the item is not a file item
String name = item.getFieldName();
String value = item.getString();
out.print("item :<b>"+name+"</b> value:<b>"+value+"</b><hr>");
}
else
{
// the item is a file item
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();

out.print("item :<b>"+fieldName+"</b><br>");
out.print(" file :<b>"+fileName+"</b><br>");
out.print(" content type :<b>"+contentType+"</b><br>");
out.print(" in memory ? :<b>"+isInMemory+"</b><br>");
out.print(" size:<b>"+sizeInBytes+"</b> bits<br>");
out.print("fileName :<b>"+fileName+"</b><br>");

fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
out.print("fileName :<b>"+fileName+"</b><br>");
String destFile="\\\\server1\\temp\\"+fileName;
out.print(" destFile:<b>"+destFile+"</b><br>");

// copy the file to disk
File uploadedFile = new File(destFile);
item.write(uploadedFile);
out.print(" file uploaded to : <b>"+ destFile +"<br>");
}
}//while end
}//try end
catch (FileUploadBase.SizeLimitExceededException e)
{
out.print("COMMONS error : maximum file size exceeded");
}
catch (FileUploadException e)
{
out.print("COMMONS error :"+ e.toString());
}
catch (Exception e)
{
out.print("error :"+ e.toString());
}
}

%>

</BODY>
</HTML>



jsut check out these and let me know
waiting for ur reply



regards
santosh
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Commons Fileupload User Guide
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

thanks for the info
i've seen the doc, is there any other possibility to catch the values from html in jsp.
can it be done using some scripts

waiting for your reply


regards
santosh
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Process a regular form field.
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi


i've done that, you can see that in the code above
the thing is, i need to upload the file depending upon the selection from the dropdown box by the user
how can i do that.

regards
santosh
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Santosh Kumar:

i've done that, you can see that in the code above
the thing is, i need to upload the file depending upon the selection from the dropdown box by the user
how can i do that.


Couldn't get you. We need input type file not the dropdown box.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Santosh, got your PM. Although, its better to move the discussion further in the same thread.

Now tell me what do you get by file upload.

1) sending file to the client/user
2) receiving file from the client/user
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

what i am doing is allowing the user to upload file from local system to the remote server...this works fine if i hardcode the path
but when i want to upload to a particular directory on the server..i need to allow user to select the dir
now i have the prob in passing the dir name in the html to jsp
u can see the code above..both html and jsp for reference

what to do
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Now I got you.

By Category you mean directory, right?

You want to place the file under particular directory and the directory/category is selected by the user. And you are getting null as category. Therefore, I have given you the code how to get form fields.

Just change all your HTML pages to JSP.
[ November 11, 2005: Message edited by: Adeel Ansari ]
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
i've already used what you said in my code
i wanted to send the user selected value and the file name to be uploaded together to the jsp page
once this is done i can upload the file to that particular directory.

does the making of html pages to jsp make any difference
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It doesn't matter if it's a JSP or HTML page.

You can send both form fields and uploaded files in the same post.
Just loop through all the 'items' and test to see if they're form fields or not.
Adeel's code shows you how to do this.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Change


to



Then let us know that are you getting the value or not?
[ November 11, 2005: Message edited by: Adeel Ansari ]
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

i am getting the form field values using that code
the thing is when i check for the directory name existing on the system and the dirctory name selected by the user to be same and try to upload the file if both are equal
i am not able to upload the file and also not able to check the directory names

what could be the problem
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

the thing is when i check for the directory name existing on the system and the dirctory name selected by the user to be same and try to upload the file if both are equal
What "system" are you talking about here?

Code on the server cannot discover anything about directory names on the client. If you want the user to select a name of a directory that is on the server that's fine, it would help to provide a drop-down box or something but there are many ways to do that. But you can't control what directory the user uploads the file from, nor should you.
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
yes
what you said is right
i want the user to select the directory on the server.for that reason i provide a dropdown box with the directory names.
once the user selects the directory name in the dropbox along with the file to be uploaded and says submit the file has to be uploaded to that particular directory on the server.

how to go with it
waiting for your reply


regards
santosh
Vivek Puthiyonnan
Greenhorn

Joined: Jan 26, 2005
Posts: 18
hi

try this:

MultipartFormDataRequest mrequest=new MultipartFormDataRequest(request);
String name=mrequest.getParameter("Name");

bye


Vivek P
Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi vivek

can u tell me where is this package MultipartFormDataRequest
can u give me some more info on this please

MultipartFormDataRequest mrequest=new MultipartFormDataRequest(request);
String name=mrequest.getParameter("Name");

waiting for your reply

regards
santosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to pass values from html to jsp