從客戶端向服務端發起請求(3種)

weixin_34247155發表於2019-01-20
  1. 通過表單向服務端發起請求

  2. 通過連結向服務端發起請求

  3. Ajax向服務端發起請求

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    out.println(basepath); // http://localhost:8080/BaseServlet/
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index page</title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    function fun() {
        $.get("<%=path%>/StuServlet", {
            "method" : "selectStu",
            "user" : "tom"
        }, function(data) {
            alert(data);
        });
    }
</script>
</head>
<body>
    <!-- 1. 通過表單向服務端發起請求 -->
    <form action="<%=path%>/StuServlet?method=addStu" method="post">
        <button>form表單post提交</button>
    </form>
    <br />
    <!-- 2. 通過連結向服務端發起請求 -->
    <a href="<%=path%>/StuServlet?method=deleteStu">超連結</a>
    <br />
    <!-- 3. Ajax向服務端發起請求 -->
    <button onclick="fun()">Ajax方式</button>
</body>
</html>

相關文章