将参数从 javascript 传递到 servlet

发布于 2024-11-08 03:08:34 字数 2440 浏览 0 评论 0 原文

我正在从 gmail 和 yahoo 检索用户的所有联系人,我添加了复选框,用户需要选择他需要向其发送电子邮件的所需电子邮件 ID。我需要收集所有用户选择的电子邮件 ID,并将它们保存在一个字符串中,将该字符串发送到我正在发送电子邮件的另一个 servlet。

我能够动态添加复选框,但无法收集电子邮件并将它们保存在字符串中。这是我编写的用于在所有电子邮件之前添加复选框的代码, 请帮助我将这些选定的电子邮件 ID 放入一个字符串中

,我使用了以下代码,但仍然无法做到这一点。您可以查看此应用程序的演示 http://ec2-50-16-183-101.compute-1.amazonaws.com/SocialAuthNew/ 要从 Gmail 中获取联系人,请在文本框中输入 google,对于 yahoo,请输入 yahoo,然后单击“提交”按钮

 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>");
                }

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>");
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时光的容颜 2024-11-15 03:08:34

试试这个:

1)将您的电子邮件包装在 DOM 元素中以使其更容易访问

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

2)使用诸如 JQuery 之类的东西标准化对客户端上 DOM 的访问,执行

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

此操作会返回数组中的电子邮件 - 如果需要,您可以轻松地将其连接成字符串,例如

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

...尽管我认为使用数组通常更好(如果需要,也许可以使用 JSON 编码)对其进行序列化)。

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).

孤者何惧 2024-11-15 03:08:34

使用数组要容易得多。我使用了数组,并将复选框名称指定为 check1,然后单击“提交”按钮,我调用了以下函数。该函数警告所选复选框的值并将操作传递给 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>

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>
你列表最软的妹 2024-11-15 03:08:34

实现此目的的一种稍微原始的方法是,

像您现在所做的那样创建一堆复选框,但不同之处在于所有 htem 应该具有相同的名称,即在代码中进行以下更正

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

现在检索此类文本的所有值使用以下请求对象调用在服务器端打开盒子,

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

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文