如何停止来自 Html 的 Servlet 重定向
How To Stop Servlet Redirect From Html
我想使用 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 Class
@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 Class(使用此 urlPatterns =“/addCustomer”)。
但我需要将数据从 HTML 网页发送到我的 Servlet Class 但我不需要重定向到 Servlet Class.
如何在不重定向的情况下停止重定向 Servlet Class 并将我的数据发送到 Servlet Class..?
在进入解决方案之前,您应该知道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>
服务器端(添加客户 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);
我想使用 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 Class
@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 Class(使用此 urlPatterns =“/addCustomer”)。 但我需要将数据从 HTML 网页发送到我的 Servlet Class 但我不需要重定向到 Servlet Class.
如何在不重定向的情况下停止重定向 Servlet Class 并将我的数据发送到 Servlet Class..?
在进入解决方案之前,您应该知道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>
服务器端(添加客户 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);