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"); } %>