checking which check boxes are selected using JAVA ( a jsp)

后端 未结 4 1416
灰色年华
灰色年华 2021-01-02 13:09

I am trying to create a servlet that displays a simple form with checkboxes , when the user selects the number of checkboxes he wants and clicks on a \"confirm\" the POST re

相关标签:
4条回答
  • 2021-01-02 13:26

    This one might be neater if you just want the output. Assuming you're using jstl libraries, which I prefer because it makes your pages cleaner:

    <c:forEach var='fruitValue' items='${paramValues.fruit}'> 
       ${fruitValue} <br>
    </c:forEach>
    
    0 讨论(0)
  • 2021-01-02 13:38

    In your servlet you would use getParameter() like so:

    request.getParameter( "id_of_checkbox" )
    

    That function returns null if the the box is not checked. So you could do something like:

    boolean myCheckBox = request.getParameter( "id_of_checkbox" ) != null;
    

    Now myCheckBox is true if checked, false if not checked.

    0 讨论(0)
  • 2021-01-02 13:43

    This is actually the HTML form behavior question. When you check a few checkboxes with one "name" attribute and different "value" attributes and press submit button, your browser will send request to the server with checked checkbox values. So you can get value names from this url parameters.

    For example:

    <form name="input" action="html_form_action.asp" method="get">
    <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
    <input type="checkbox" name="vehicle" value="Car">I have a car 
    <br><br>
    <input type="submit" value="Submit">
    </form>
    

    If you check both of checkboxes your server will receive this parameters like so:

    http://example.com/your_page.jsp?vehicle=Bike&vehicle=Car 
    

    After that you can get values like this:

    String checkboxValues = request.getParameter("vehicle");
    

    checkboxValues gets all values separated by comma.

    0 讨论(0)
  • 2021-01-02 13:47
    <%@ page language="java"%>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>JSP Multiple Checkbox</title>
        </head>
        <body>
            <form name="form1" onsubmit="checkBoxValidation()">
                <h3>Select your favorite Fruits</h3>
                <p><input type="checkbox" name="fruit" value="Mango"/>Mango</p>
                <p><input type="checkbox" name="fruit" value="Apple"/>Apple</p>
                <p><input type="checkbox" name="fruit" value="Grapes"/>Grapes</p>
                <p><input type="checkbox" name="fruit" value="Papaya"/>Papaya</p>
                <p><input type="checkbox" name="fruit" value="Lychee"/>Lychee</p>
                <p><input type="checkbox" name="fruit" value="Pineapple"/>Pineapple</p>
                <p><input type="submit" value="submit"/>
            </form>
            <%String fruits[]= request.getParameterValues("fruit");
            if(fruits != null){%>
            <h4>I likes fruit/s mostly</h4>
            <ul><%for(int i=0; i<fruits.length; i++){%>
                <li><%=fruits[i]%></li><%}%>
            </ul><%}%>
        </body>
    </html>
    

    Run this sample jsp on your web container to get some basic idea on how it works. You need to move the display logic on this page that gets request parameter into your servlet code on form submission. This example can be found from here. Hope this would help.

    0 讨论(0)
提交回复
热议问题