0

I am retrieving all the contacts of a user from gmail and yahoo, I have added the checkbox, the user needs to select the desired email id to which he need to send email. I need to collect all user selected email id's and save them in a string send that string to another servlet where I am sending emails.

I was able to add the check box dynamically but I am not able to collect the emails and save them in a string. This is the code I have written to add check box before all the emails, kindly help me to put those selected email id's in a string

I used the following code, but still I am not able to do it.You can have a look at the demo of this app http://ec2-50-16-183-101.compute-1.amazonaws.com/SocialAuthNew/ To get the contacts from Gmail type google in the text box and for yahoo type yahoo and click on submit button

 List<Contact> contactsList = provider.getContactList();
                    PrintWriter out = response.getWriter();  
                    out.println("<html>");
                    out.println("<head><script type='text/javascript'>");
                    out.println("function getAllContacts(size){ var selected_list='';");
                    out.println("for(var c=0;c<size;c++){if(document.getElementById('mailCheckbox'+c).checked==true){selected_list=selected_list+document.getElementById('lblmail'+c).innerHTML+':';}}");
                    out.println("document.getElementById('final_mailing_list').innerHTML=selected_list;}</script>");
                    out.println("<title></title>");
                    out.println("</head>");
                    out.println("<body>");

                    for(int i=0;i<contactsList.size();i++){
                        System.out.println(contactsList.get(i).getFirstName()+" : "+contactsList.get(i).getLastName()+":"+contactsList.get(i).getEmail());

                        out.println("<h1> Imported conatcts from your mail are:-</h1>");
                        out.println("<input type='checkbox' id='mailCheckBox"+i+"' name='mailCheckbox'></input>");
                      /*  out.println(contactsList.get(i).getFirstName());
                        out.println(contactsList.get(i).getLastName());*/
                        out.println("<label id='lblmail"+i+"'>"+contactsList.get(i).getEmail()+"</label>");
                    }
                    int size=contactsList.size();
                    out.println("<input type='button' value='GetContact' onclick='getAllContacts("+size+");'/> ");
                    out.println("<div id='final_mailing_list'></div></body>");
                    out.println("</html>");
                }
1
  • HTML belongs in JSP, not in servlet. It'll make your life (and JavaScript stuff) A LOT easier. Commented May 18, 2011 at 12:11

3 Answers 3

3

Try this:

1) Wrap your email in a DOM element to make it easier to access

out.println("<span>" + contactsList.get(i).getEmail() + "</span>");

2) Using something like e.g. JQuery for normalizing access to the DOM on the client, do

function getSelectedEmails() {
  var emails = [];
  $('body').find('input[name="mailCheckbox"]:checked').each(function() {
    emails.push($(this).closest('span').text());
  });
  return emails;
}

This returns the emails in an array - which you can easily concatenate into a string if you want with e.g.

var emailString = emails.join(", ");

...although I think using an array is usually better (perhaps JSON encoded if you need to serialize it).

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

1 Comment

Hello I made some changes in the code. and I have posted here, kindly have a look and please help me.
1

s using array is much easier. i have used array and gave the check box name as check1 and on the click of submit button i have called the following function. this function alerts the value of the selected check boxes and passes the action to servlet

<script>
function onCallForInterview()
{
var selectedIds;
var count=0;
for (i=0; i<document.frm.check1.length; i++)
{
if (document.frm.check1[i].checked==true)
{
if(count==0){
selectedIds=document.frm.check1[i].value; 
count=count+1; 
}
else
selectedIds=selectedIds+","+document.frm.check1[i].value;
}
}
alert(selectedIds);  
document.frm.action="<%=contextPath%>/SearchCandInfo?    action=selectcanforinterview&ids="+selectedIds;
document.frm.submit();
}
</script>

3 Comments

thanks for the help I tried the logic which u suggested, I guess the I have done something wrong. I am not able to get the desired out put. Kindly have a look.
just call this function onclick='getAllContacts()'; and in the function u should take the length of the check box ie mailcheckbox.length() try this i have used array in my prog and it is working fine but u r using for loop so check if the above suggestion helps u .
<input type='checkbox' id='mailCheckBox"+i+"' name='mailCheckbox'> in this line add something like this value="contactsList.get(i).getEmail()" so that each check box created dynamically will have a unique value
1

A slightly more primitive way to achieve this,

Create a bunch of Check-Box as you are doing now but with the difference that all of htem should have the same name i.e. do the following correction in your code

out.println("<input type='checkbox' id='mailCheckBox' name='mailCheckbox'></input>");

Now retrieve all the values of such text boxes on your server side using following call on request object,

            String[] emailIds = request.getParameterValues("mailCheckBox");

Hope this helps.

2 Comments

It is not valid HTML to have multiple DOM elements with the same ID.
@Jeevan: You need not do any change in your html generation code. Just use it. My approach just need the same parameter name. IDs could be different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.