jsp作为服务端,ajax请求回应

刚学ajax,想以jsp作为服务端,来回应ajax的请求:

代码如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../js/jquery.min.js" ></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btn").on("click", function () {
                $.ajax({
                    url:"http://localhost:8080/StrutsExercise/server.jsp",
                    data:{name:$("#tid").val()},
                    type:"get",
                    datatype:"html",
                    success:function(data){
                        $("#pid").text(data);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <input type="text" id="tid"/>
    <button id="btn">send</button>
    <p id="pid">result</p>
</body>
</html>

server.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<% String name = request.getParameter("name");
    if(name.equals("admin")){
        response.getWriter().println("login:"+name);
    }else{
        response.getWriter().println("login:error");
    }
%>

 

posted @ 2016-05-28 20:11  构建巨人肩膀  阅读(885)  评论(0编辑  收藏  举报