如何发送此选中的项目以显示在另一页上? JSP SERVLET

时间:2019-05-17 19:06:37

标签: jsp servlets

我对JSP Servlet不太熟悉。 单击此“添加”按钮时,应将这些值发送到清单中,而选中的项目应在单击“完成”按钮后显示在另一页上。任何帮助将不胜感激。

我尝试了以下代码。

这是我的handle.jsp代码。

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


    }

}

1 个答案:

答案 0 :(得分:0)

为什么要使用选项按钮作为检查清单,所以应该使用复选框。提交表单后,您应该在servlet中检索值。

我正在做一个项目,并且按照您的要求进行了操作。

  

Index.jsp

用户检查其公司的所有实力,然后单击提交。

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

现在在servlet中,检索值。

  

Swot.java

如果value属性包含一个字符串,请执行此操作。

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

        PrintWriter out=response.getWriter();


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

现在所有值都以数组强度存储。 如果value属性包含整数,则需要进行强制转换。

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

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

    }

快乐编码。