<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'adjx.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/jquery-3.3.1.js"></script> <script type="text/javascript"> $(function(){ $("#submit").click(function(){ var param = { name:$("#name").val(), age:$("#age").val() }; $.ajax({ data:param, url:"oneServlet", beforeSend:function(){ $("img").show(); }, complete:function(){ $("img").hide(); }, success:function(data){ alert(data); }, error:function(XMLHttpRequest, textStatus, errorThrown){ alert(XMLHttpRequest.status+" "+textStatus+" "+errorThrown); } }); }); }); </script> </head> <body> <form> <table> <tr><td>name:</td><td><input type="text" id="name" name="name"></td></tr> <tr><td>age:</td><td><input type="text" id="age" name="age"></td></tr> <tr><td><input type="button" value="submit" id="submit"></td></tr> </table> <img src="images/6.jpg"> </form> </body> </html>
servlet:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class oneServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String age = request.getParameter("age"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(name+":"+age); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
************************************************************************************************************************************************************************************************