Delete data dynamically with JSP and servlet (Very Urgent)
Hi,
I am new with servlets and JSP. On my jsp page I am populating customer names in a drop box from database and I have 3 buttons ADD, DELETE and EDIT.
Now when I press DELETE buttong I am calling delete function which is writen in Javascript on my jsp file only,now I want to pass these selcted values ehich I have stored in an array to be pass to servlet,so I can write Delete query for it and delete.
Could anyone help with sample code>
Here what I am trying to say:
<HTML>
<HEAD>
<script language="JavaScript" type="text/javascript">
<!--
function remove()
{
var u = confirm("Are you sure you want to Delete Selected names?")
if (u==true)
{
var selectedArray = new Array();
var selObj = document.getElementById('CUSTOMER_NAME');
var i;
var count = 0;
for (i=0; i<selObj.options.length; i++) {
if (selObj.options.selected) {
selectedArray[count] = selObj.options.value;
count++;
}
}
alert("names to be deleted are:"+selectedArray);
// selectedArray has the selected items stored
alert("Items Deleted!")
}
else
{
alert("No data were deleted!!You pressed Cancel!")
}
}
//-->
</script>
</HEAD>
<BODY>
//calling servlet on action tag of form
<FORM METHOD= 'POST' ACTION='Remove_Customer'>
<form ><INPUT TYPE='submit' NAME='DELETE' VALUE='DELETE' onclick="remove()"></TD></form>
<form><INPUT TYPE='submit' NAME='MODIFY' VALUE='MODIFY' onclick="modification()" ></TD></form>
</BODY>
</HTML>
PLEASE HELPME,it's very urgent.
Since the selected Array is in javascrip language , I don't know how can I pass that to servlet.
request.getParameter is not working,since it's an array
Many thanks in Advance.
[2055 byte] By [
ASH_2007a] at [2007-11-27 6:36:26]

# 1
JavaScript can't communicate directly with JSP since it is client-side while JSP is server side.
What you could do is put this list in hidden fields so that the servlet can access them.
But a better idea would be to submit the page when the user selects to delete and delete the data in the servlet and then redirect back to this page.
With your way, the user might see that the data is deleted but it may not actually be; what happens if the network goes down? Your user wil be able to delete stuff since you're doing all this on the client-side, but actually the data is not getting affected at all!
# 2
Thanks a lot for the useful information!
I did put hiiden field in jsp but how can I stored the array in hiidel field.
what I did is:
document.getElementById("Selected_names").value= selectedArray;
Selected_names is the hidden field.
Its displaying the right names seperated by comma, but how can I access that from servlet.
Let me know.
Thanks
# 3
> Selected_names is the hidden field.
> Its displaying the right names seperated by comma,
> but how can I access that from servlet.
Well, now when your page is submitted, you can use request.getParameter("Selected_names") in the servlet/ JSP, it'll give you a string with these comma separated values.
P.S. You should make sure you don't get a null from getParameter() which will happen if you try to get a field which doesn't exist or misspell the fieldname.
# 4
Yeah, I figured it out rite after I post it. Sorry about that.
But thank you for the information. You are just wonderful!
But have one mmore problem. I called it with request.parameter but how Do I seperate the names.
I mean I want to delete those names with my delete query.
And right now string is like: abc,bcd,efg.
Can you help?
Thanks :-)
Ash
# 5
Cross post: http://forum.java.sun.com/thread.jspa?threadID=5180679
# 6
Hi there, Sorry about that, But I am new to forum and i coudn't figure how to delete that.Once again I apologies for that.When I first posted it was 2 different problems I was having but now it's all combined.Could you help me?please..Thanks You
# 7
:)) You got it wrong; topics won't be deleted unless the moderators decide to, I was just marking it there to combine both. So that others, when they view this topic, will know that the question is also on another topic and that's where it's being answered from now. So check there :)
# 8
Hi,
you can use StringTokenizer for that purpose.the comma is a delimeter in your scenario, so use that one.and the string is hidden filed array.
StringTokenizer st = new StringTokenizer(string,",");
Names = new String[st.countTokens()];
int i=0;
while(st.hasMoreTokens())
{
Names[i++] = st.nextToken();
}
here Names is ur deletenames String array.
Names = new String[st.countTokens()];
in this line , I am iassigningthe size of Names Array dynamically depends upon the String u passed.for example if The string is "hai, bye, go"then the length of Names array is 3(by deleting commas)
In next line, Iam intializing the Names array with deletedemps using
Names[i++] = st.nextToken();
hope you undersatnd this concept!
# 9
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
From http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
Also, cross post : http://forum.java.sun.com/thread.jspa?threadID=5180679
# 10
Hey,
I am having problem deleting multiple values.
If I just select one value,it deletes it
But for multiple values the code is not working right.
If there 2 values selcted then the last value gets deleted,if there are more than 2 values selected none values delete.
Could plese check the code,and let meknow what mistake am I doing?
Please help
String delName="";
int pos = tStr.indexOf(",");
System.out.println(pos);
if (pos != -1)
{
delName = tStr.substring(0, tStr.indexOf(","));
tStr = tStr.substring(tStr.indexOf(",") + 1 );
System.out.println("DElName:"+delName);
System.out.println("tStr::"+tStr);
}
delName = tStr;
delete_Customer(delName);
Thanks
ASH
# 11
You're only checking for one value and then replacing the delete string with the complete string so it's only deleting that...you need to put it into a loop and extract each value and delete one by one till the string is empty.
String delName=request.getParameter("arrayOfNames");
int pos = 0;
pos=tStr.indexOf(",");
while (pos != -1)
{
System.out.println(pos);
delName = tStr.substring(0, tStr.indexOf(","));
tStr = tStr.substring(tStr.indexOf(",") + 1 );
System.out.println("DElName:"+delName);
System.out.println("tStr::"+tStr);
delete_Customer(delName);
pos=tStr.indexOf(",");
}
I think you need to clear your basics on loops and logic.
# 12
Thank You,That was such a stupid mistake.Thanks a lot
# 13
Hi noggodatprogramming, I have one more question.How can I trim spaces in my string in Javascript?I din't see function like trim() in javascript, so could you suggest me anything?PleaseTHanks for your help,ash
# 14
> Hi noggodatprogramming,
:)) its nogoodatcoding
> I have one more question.
It's not considered good manners to direct questions to specific members unless it's to clarify a previous point. :) People might take offence; the forums are meant for everyone to interact.
> ow can I trim spaces in my string in Javascript?
This is a Java forum; JavaScript doesn't have anything to do Java at all! Except for the first four characters :D
> I din't see function like trim() in javascript, so
> could you suggest me anything?
I also needed this quite often and I found this code somewhere, can't credit the author since I don't know where it's from, but it's pretty useful:
function trim(str)
{
str = this != window? this : str;
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
Hope this helps :)
# 15
Hey,
Actually,
it's not a string it's array of Javascript:
I am passing those values to a string in servlet.
So I want array values to be trimmed.
Si it possible:
This is what I am saying:
var selectedArray = new Array();
var selObj = document.getElementById('CUSTOMER_NAME');
for (i=0; i<selObj.options.length; i++) {
if (selObj.options.selected) {
selectedArray[count] = selObj.options.value;
count++;
}
}
alert("names to be deleted are:"+selectedArray);
document.getElementById("Selected_names").value= selectedArray;
alert(Selected_names.value)
I am assigning selectedarray to selected_names.value and then passing it to servlet, so How can I trim it?
IF i enter "ABC SS" it only passes ABC.
plz help
Thanks
Message was edited by:
ASH_2007>
# 16
>alert(Selected_names.value)
Does this show the correct values?
If 'trim' the final string, i.e. remove the spaces, how will you be able to separate them on the server side?
Why don't you put them in a string separated by a , or something in the first place?
> var selectedArray = new Array();
> var selObj =
> j = document.getElementById('CUSTOMER_NAME');
var finalString = "";
>
>for (i=0; i<selObj.options.length; i++) {
>if (selObj.options[i].selected) {
> //selectedArray[count] = //you didn't need to use count, you could just use i again here
> //ount] = selObj.options[i].value;
finalString = finalString + "," + selObj.options[i].value;
>//count++;
>}
>}
>alert("names to be deleted are:"+selectedArray);
> document.getElementById("Selected_names").value=
> selectedArray;
>alert(Selected_names.value)
And use code tags, it's not difficult. Just select the text and press the button that says 'code' while posting.
# 17
> >alert(Selected_names.value)
> Does this show the correct values?
Yes, it working correct!
> If 'trim' the final string, i.e. remove the spaces,
> how will you be able to separate them on the server
> side?
That's a good point, and I don't know how I am going to do that :-)
Didn't realize at all, any suggestions for that?
Follow the code you gave me ,but it's printing : ABC,
for "ABC DEF"
:-(
# 18
I am sorry,It's printing ,ABC (a comma and then ABC)
# 19
Sorry, my mistakeIt should be:finalString = finalString + selObj.options[i].value + ",";This will give you a string with one , extra, at the end; figure out the logic to extract it. Use the split() method of the String class in JSP
# 20
I'm coming onto this thread late.You can use req.getParameterValues("selectName") to get multiple values in a servlet which returns a String[]. I also suggest you use the String.split method instead of StringTokenizer.
# 21
That is still printing ABC,It's not consideing space between words :-(
# 22
> I'm coming onto this thread late.
> You can use req.getParameterValues("selectName") to
> get multiple values in a servlet which returns a
> String[].
> I also suggest you use the String.split method
> instead of StringTokenizer.
I am not using that values into srevlet, this is before I call servlet I am opening a new popup window (another Jsp Page)where I want values from the orginal JSP page(from where I ma opening it)
And from that popwindow I'll be calling servlet.
So right now dillema is how to extract data of one jsp page into another jsp page
Any help/suggestion will be appreciated
Thanks
# 23
You are running it in the same loop that you had earlier right? Just replacing the code to populate the array with this code to make the string?
# 24
Yeah That's right.But it's still not taking the spaces. :-(It's jst taking the string before space,it's avoiding anything after spaceWhat should I do? Plz Help
# 25
Can post the code that you're using now?Just trim it to cut out all the unecessary portions. Just leave the HTML fields and the JavaScript function. And use code tags :D
# 26
function remove()
{
var selectedArray = new Array();
var selObj = document.getElementById('CUSTOMER_NAME');
var finalString = "";
var i;
var count = 0;
for (i=0; i<selObj.options.length; i++) {
if (selObj.options[i].selected) {
// selectedArray[count] = selObj.options[i].value;
// count++;
finalString = finalString + selObj.options[i].value + "," ;
//selObj.options[i].value;
}
}
alert("names to be deleted are:"+finalString);
document.getElementById("Selected_names").value= finalString;
alert(Selected_names.value)
}
HTML:
><TD VALIGN='top'><INPUT TYPE='submit' NAME='DELETE' VALUE='DELETE' onclick="remove()"></TD>
<TD VALIGN='top'><INPUT TYPE='submit' NAME='MODIFY' VALUE='MODIFY' onclick="modification()" ></TD>
<TD><input type="hidden" id="Selected_names" name="Selected_names"> </TD>
Thanks (Have used code :-)) )
# 27
I'm sorry, it's been sometime since I saw your code so I've forgotten details.
Can you tell me what kind of an HTML element is CUSTOMER_NAME? Is it a series of check boxes ?
If yes, then I don't know if you've already done this but it should be something like this:
1. All the checkboxes should have the same name ( which is allowed ) but not the same id ( which would be wrong, id is supposed to be unique ).
2. Then you can use getElementsByName() which will return an array of the elements ( checkboxes here ) and then you cycle through that.
With getElementById() when there are multiple elements with the same id, the behaviour is undefined, but as far as I know, you'll get the first element that the browser decides is the right one.
I think your problem was that you were only getting one value in the final string right? I think this might be the problem.
# 28
> I'm sorry, it's been sometime since I saw your code
> so I've forgotten details.
It's okay.
> Can you tell me what kind of an HTML element is
> CUSTOMER_NAME? Is it a series of check boxes ?
CUSTOMER_NAME is an OPTION (SELECT). (so it's a drop box, where you can select multiple options)
ANd it does work with getElementByID, but if there is a space it doesn't take it.
For example, if selected names are: ASH 2007, ABCD, GHIY
Then it takes ASH ,ABCD,GHYI
Here I am posting my code. getElementsByName isn't working at all
function remove_data()
{
var u = confirm("Are you sure you want to Delete Selected names?");
if (u==true)
{
var selectedArray = new Array();
var selObj = document.getElementById('CUSTOMER_NAME');
alert(selObj);
var finalString = "";
var i;
var count = 0;
for (i=0; i<selObj.options.length; i++) {
if (selObj.options[i].selected) {
// selectedArray[count] = selObj.options[i].value;
// count++;
finalString = finalString + selObj.options[i].value + "," ;
//selObj.options[i].value;
}
}
alert("names to be deleted are:"+finalString);
document.getElementById("Selected_names").value= finalString;
alert(Selected_names.value)
}
else
{
alert("No data were deleted!!You pressed Cancel!")
}
}
//-->
</script>
HTML****
<TD VALIGN='top'><INPUT TYPE='submit' NAME='DELETE' VALUE='DELETE' onclick="remove_data()"></TD>
<TD><input type="hidden" id="Selected_names" name="Selected_names"> </TD>
</TR></TABLE></FORM>
Thanks
# 29
So in short, my problme is with space.Space between words.Datas without space gets deleted, and obviously data with space couldn't be found,since it just takes word before space
# 30
Ok, if it's a combo box then getElementById() is correct.
I'm stumped as to what the problem could be. Will you check that your 'values' for the <option> tags are between double quotes? This is the only case I know where the spaces are ignored.
As in, it should be "ASH 2007" and "ABCD"; if it's ASH 2007, only ASH will be picked up. This is the only thing I can think of.