-2

I'm not very familiar with JSP Servlet. When clicking this "Add" button, it should send those values to checklist and those checked items should Display on another page after clicking "Done" button.Any help would be greatly appreciated.

I tried following codes.

This is my handle.jsp code.

<body>

<div id="myDIV" class="header">
        <form action = "Handle_servlet" method = "POST">
        <div class = "hd" >
            <h2 style="margin:10px;float:center;text-align:left;">INCOMING CALLS</h2>
                <input type="text" id="myInput" placeholder="Call ID...">

                    <select class="input100" name="iname" id="myInput1" >
                        <option value="Contact an agent">Contact an agent</option>
                        <option value="option1">option1</option>
                        <option value="option2">option2</option>
                        <option value="option3">option3</option>
                    </select>

                <span onclick="newElement()" class="addBtn">Add</span>

                <ul id="myUL" name = "chklist">



                </ul>
                <span class="addBtn">Done</span>


        </div>
        </form>
</div>





<script>
        // Create a "close" button and append it to each list item
        var myNodelist = document.getElementsByTagName("LI");
        var i;
        for (i = 0; i < myNodelist.length; i++) {

        var span = document.createElement("SPAN");
        var txt = document.createTextNode("\u00D7");
        span.className = "close";
        span.appendChild(txt);
        myNodelist[i].appendChild(span);

        }

        // Click on a close button to hide the current list item
        var close = document.getElementsByClassName("close");
        var i;
        for (i = 0; i < close.length; i++) {

        close[i].onclick = function() {
        var div = this.parentElement;
        div.style.display = "none";
        }
        }   

        // Add a "checked" symbol when clicking on a list item
        var list = document.querySelector('ul');
        list.addEventListener('click', function(ev) {
        if (ev.target.tagName === 'LI') {
        ev.target.classList.toggle('checked');
        }
        }, false);

        // Create a new list item when clicking on the "Add" button


        function newElement() {

        var li = document.createElement("li");
        var inputValue = document.getElementById("myInput").value;
        var drop = document.getElementById("myInput1").value;
        var t = document.createTextNode(inputValue);
        var x = document.createTextNode(drop);

        li.appendChild(t);
        //li.appendChild(x);
            if (inputValue === '') {
                alert("You must write something!");
                }
                    else {
                        document.getElementById("myUL").appendChild(li);
                         }

                        document.getElementById("myInput").value = "";
                        document.getElementById("myInput1").value = "";

        var span = document.createElement("SPAN");
        var txt = document.createTextNode("\u00D7");

        span.className = "close";
        span.appendChild(txt);
        li.appendChild(span);

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}

</script>

</body>

Handle_servlet.java

package Controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class Handle_servlet extends HttpServlet
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException    
    {
        String[] chklist = req.getParameterValues("chklist");
        List list = Arrays.asList(chklist);

        req.setAttribute("chklist", list);
        RequestDispatcher rd = req.getRequestDispatcher("epp.jsp");
        rd.forward(req, res);


    }

}
  • check out [this](https://stackoverflow.com/questions/23713329/trying-to-get-some-kind-of-dynamic-unordered-list-to-send-the-order-to-servlet). – Swati May 18 '19 at 14:55

1 Answers1

-1

Why are you using option button for checklist, you should be using checkbox. And after form is submitted, you should retrieve the values in servlet.

I was working on a project and did the same what you are asking for.

Index.jsp

User checks on all the strength of his company and click submit.

 <form action="swot" method="post">
    <div class="strength" style="border: 3px solid crimson ; ">
        <div class="panelhead">
        <center>   <h3>Strength</h3></center>
        </div>
    //give value property according to your needs.
        <p><input type="checkbox" name="strength" value="10"/>Brand Name</p>
        <p><input type="checkbox" name="strength" value="8"/>Customer Loyalty</p>
        <p><input type="checkbox" name="strength" value="9"/>Technology</p>
        <p><input type="checkbox" name="strength" value="9"/>Unique Products</p>
        <p><input type="checkbox" name="strength" value="7"/>Supply Chain</p>
        <p><input type="checkbox" name="strength" value="8"/>Innovative Culture</p>
        <p><input type="checkbox" name="strength" value="7"/>Cost Advantages</p>
        <p><input type="checkbox" name="strength" value="6"/>Size Advantages</p>
        <p><input type="checkbox" name="strength" value="8"/>Financial Leverage</p>
        <p><input type="checkbox" name="strength" value="9"/>Economics of Scale</p>

    </div>
            <center><input class="submit" type="submit" value="Submit"/></center>
    </form>

Now in the servlet, retreive the values.

Swot.java

If the value property contains a string just do this.

public class swot extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out=response.getWriter();


        String strength[]= request.getParameterValues("strength");

Now all the values are stored in array strength. If the value property contains integer, you need to typecast it.

   int[] mat = new int[strength.length];

    for(int i = 0; i < strength.length; i++)
    {
        mat[i] = Integer.parseInt(strength[i]);

    }

Happy Coding.

Abhishek P
  • 58
  • 1
  • 12