有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript如何从Html停止Servlet重定向

我想使用Servlet将数据从Html网页发送到我的服务器

<form method="post" name="customerForum" action="addCustomer">
  <button class="btn btn-success" id="addCustomerBTN"
         type="submit" value="addCustomer">Add Customer
 </button>
</form>

这是我的Servlet类

@WebServlet(urlPatterns = "/addCustomer")
public class PSystemServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String customerID = req.getParameter("custID");
        String customerName = req.getParameter("custName");
        
    } 
}

如果我按下按钮,网页重定向到我的Servlet类(使用这个urlPatterns=“/addCustomer”)。 但我需要将数据从HTML网页发送到我的Servlet类,但不需要重定向到Servlet类

如何停止重定向Servlet类并将数据发送到Servlet类而不重定向


共 (1) 个答案

  1. # 1 楼答案

    在开始使用解决方案之前,您应该了解AJAX及其工作原理

    试试这个: 客户端:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    
    
        <body>
            <form method="post" name="customerForum">
                CID:  <input type="text" id="custID" name="custID">
                CNAME:<input type="text" id="custName" name="custName">
                <button class="btn btn-success" id="addCustomerBTN"
                        type="button" value="addCustomer" onclick="register()">Add Customer
                </button>
            </form>
        </body>
    
        <script>
    
            function register()
            {
    
                var custName = document.getElementById('custID').value;
                var custID = document.getElementById('custName').value;
             
                $.ajax({
                    type: "GET",
                    url: "addCustomer",
                    data: {custID: custID, custName: custName},
                    success: function (status) {
                        
                        alert(status);//You can add if codition to check status values and alert msg accordingly if 'inserted' then alert data inserted sucessfully
                    }
                });
    
    
    
            }
    
        </script>
    
    </html>
    

    服务器端(addCustomer Servlet):

            String status="notInserted";
            String customerID = request.getParameter("custID");
            String customerName = request.getParameter("custName");
            System.out.println("custID:" + customerID);
            
            //Your insert Logic
            
            //if data inserted sucessfully  set status="inserted";
                status="inserted";
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(status);