This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes How to pass more than 1 parameters into the sevlet??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass more than 1 parameters into the sevlet???" Watch "How to pass more than 1 parameters into the sevlet???" New topic
Author

How to pass more than 1 parameters into the sevlet???

Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi,
i have problem in passing 2 parameters into my servlet. can i write a JSP script inside a javascript??
below r part of my codes...
<%
String userId = (String) session.getAttribute("theName");
String url = "http://localhost:8080/SDC1/servlet/parserupload?Username="+userId;
%>
<div id="Layer8" style="position:absolute; width:753px; height:115px; z-index:9; left: 142px; top: 270px;">
<FORM name="form1" ACTION=<%=url%> METHOD="POST" ENCTYPE="multipart/form-data">
<table bgcolor="#ECDFCE" border="0" width="100%">
<tr>
<td width="643" > <strong><font color="#65226A" face="Verdana, Arial, Helvetica, sans-serif">Which
file to upload?</font></strong><font face="Verdana, Arial, Helvetica, sans-serif"> 
</font><font face="Georgia, Times New Roman, Times, serif">
<input type="FILE" name="fileName1" size="60">
</font> </td>
</tr>
.
.
.
can anybody tell me how to pass the textfield "fileName1" to action in the form?? If can, can show me an example.
Thanx in advance!!!
Torsten Schippel
Ranch Hand

Joined: May 09, 2003
Posts: 62
Hi Ann,
I'm not sure I got your problem. I think if you POST your form the userId parameter will not be recognized by your servlet. So you need to add the userID into the form. Just use a hidden form field. Then it is just a standard submit.
Regards
Torsten
Sudhakar Reddy
Greenhorn

Joined: Aug 28, 2003
Posts: 9
try
request.getParameter("filename1")
in servlet to get value of filename1 filed
Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi Torsten Schippel & Sudhakar Reddy Konda
thanx for ur help....but i try both of ur ways, still cannot....i can get the userid without having a hidden form but i can't get the fileName1. I did try many ways in the servlet to get the parameter but it return a null, i tink it doesn't pass in the value into the servlet.
does anybody noe how to concaterate the string url with the fileName1???
Thanx in advance!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Your problem may not be in submitting the data but in retrieving the data. The fact that you are using a mutli-part form complicates matters quite a bit. What are you using to parse the multi-part form? That may be where the problem lies.
bear
[ September 25, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
If I understand clearly what you are trying to do is to uplad a file; basically the value in the textfield fileName1 is the name of the file to be uploaded. I advice you get a handle to the JSP file say jspRef and use the method getFileName() ie jspRef.getFileName() to get the value of the text field fileName1...getFileName() returns a string which is the name of the file to be uploaded.
Anselm Paulinus
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
Oops; since the name of your field is fileName1 or so; it should read jspRef.getfileName1() and not jspRef.getFileName().
Anselm
Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi Bear Bibeault & Anselm Paulinus,
thanx for ur help...
To Bear Bibeault,
i ready hav prob in retrieving the data...i am using MultipartRequest to upload the file.
To Anselm Paulinus,

i hav try ur method but i still doesn't work. i tink it is the concaterate part. i wan to concaterate the fileName1 with the string url below. but how do i concaterate??
<%
String userId = (String) session.getAttribute("theName");
String url = "http://localhost:8080/SDC1/servlet/parserupload?Username="+userId;
%>
<div id="Layer8" style="position:absolute; width:753px; height:115px; z-index:9; left: 142px; top: 270px;">
<FORM name="form1" ACTION=<%=url%> METHOD="POST" ENCTYPE="multipart/form-data">
Thanx in advance...

[ September 25, 2003: Message edited by: Ann Toh ]
Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi,
can anybody tell me how to pass a textfield value to a JSP scriptlet???
Thanx in advance...
SaSaS
Greenhorn

Joined: Sep 26, 2003
Posts: 5
did u tried using request.getparameter("txtBoxName");
Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi SaSaS,
i did try that method, but it return a null. It does not pass in the value into the servlet. Do u hav any other method to do??
Thanx in advance....
Torsten Schippel
Ranch Hand

Joined: May 09, 2003
Posts: 62
Hi Ann,
I just tested your problem. It was a little strange to me. If I add a paramter to the URL, use POST method and ENCTYPE="multipart/form-data" I can access the added parameter through getParameter(..) but I can not access any other paramters which are part of the form (as expected because of encoding).
If you already use a multipartrequest class then it should be able to return the filename (all form parameters). Check http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html
But there are many others out there.
To have a clean design I would use a hidden field anyway. Just to separate normal GET and POST methods.
Regards
Torsten
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
Hi
Have a look at this
----------
Sainudheen
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Sainudheen, I'm not sure how that link is relevant.
Ann, as Torsten already mentioned, you *must* use a special handler for multipart/form-data. The usual method of retrieving parameters by calling request.getParameter("foo") WILL NOT WORK with multipart/form-data. The reasons are complicated but if you do a Google search for "multipart/form-data" and JSP you will find plenty of explanations/tutorials on why this is so.
If you want access to all your form parameters as well as the file you are uploading, you MUST use a library like Torsten has provided to retrieve the data from the request. In fact, this is the library I use myself.
[ September 28, 2003: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi Torsten Schippel,
I can get the filename from the multipartrequest method but i wan to check whether the textfield is empty before i upload the files. When i uploading the files, i will first delete all the files in that directory. so at this moment, when i upload a file, the 1st thing i do is to delete the files in tat directory then i upload the file and when the textfield is empty, it still delete away the existing files in the directory and return an error message.
Can anyone tell me how??
thanx in advance...
Ann Toh
Ranch Hand

Joined: Jul 18, 2003
Posts: 45
Hi Torsten Schippel and everyone,
thanx for all ur help...now i can solve my prob already. this is what i do, first i upload the files, then i get the filename from the MultipartRequest method. next i delete all the files in the directory except the files that i juz upload in it. finally, solve the prob...
thanx everyone
 
 
subject: How to pass more than 1 parameters into the sevlet???
 
Similar Threads
how to connect to database and display query vqlus using proper java/jsp
SQL and JSP
INCLUDES.DBconnection error
Confused on validate() method
Help with JSP and Form INput