开发者

Generating dynamic checkboxes through servlets?

开发者 https://www.devze.com 2023-03-17 02:33 出处:网络
I am using RestfbApi to fetch my friends\' names and display on a web page. I also want a corresponding checkbox to each friend, since from the use of table tag , I have friend\'s name as well as corr

I am using RestfbApi to fetch my friends' names and display on a web page. I also want a corresponding checkbox to each friend, since from the use of table tag , I have friend's name as well as corresponding checkbox.

As I generated checkboxes dynamically , how to make sure which checkboxes are checked when my app runs. The scenario is if I checked five friends and press post to wall button, a wall should be post to all five friends. I know how to post a wall just want to know how to map user selected friends(checked ones) to java code.

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException    
{


    User user = facebookClient.fetchObject("me", User.class);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();


    String docType =
        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
        "Transitional//EN\">\n";
        out.println(docType +
        "<HTML>\n" +
        "<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
        "<BODY BGCOLOR=\"#FDF5E6\">\n" +
        "<H1></H1>\n" +
        "</BODY></HTML>");




 JsonObject accounts = facebookClient.fetchObject("me/friends",JsonObject.class);
                  JsonArray data = accounts.getJsonArray("data");

                for(int i = 0; i < data.length(); i++)
                      {
                     String id = data.getJsonObject(i).getString("id");
                 Double int1 = Double.parseDouble(id);
                     String strName = data.getJsonObject(i).getString("name");

                   out.println("<table>");                             

                             out.println("<tr><td> <input type='checkbox' name='wahtevername' value='"+int1 +"'>"+strName+" </td></tr>");  

              开发者_运维知识库           out.println("</table>" );  

                       } 

                     }
 out.println(docType +"<form method=\"GET\">"+"<input type=\"submit\" value=\"Post to wall\" name=\"option\">"+"</form>");                         


Use request.gatParameterValues("whatevername") to get an array of the values of selected checkboxes.

(Btw, it would be better to place the html code in JSP. Set the list as request attribute and then use JSTL's <c:forEach> to iterate)

0

精彩评论

暂无评论...
验证码 换一张
取 消