html页面:
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- 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 'form_test.jsp' starting page</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <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">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- <!-- 导入 jQuery 库 -->
- <script type="text/javascript" src="jquery-1.7.2.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $("#sedd_ajax").click(function(){
- var params = $("input").serialize();//序列化表单的值key=value形式
- alert(params);
- $.ajax(
- {
- url:"Ajaxservlet", //发送到后台处理的地址
- type:'post', //提交方式
- data:params, //传递的参数
- dataType:'json', //接受数据格式
- success:function(data){
- },
- error : function(XMLHttpRequest, textStatus, errorThrown) {
- alert(XMLHttpRequest.status); //200
- alert(XMLHttpRequest.readyState); //4
- alert(textStatus);//解析文本状态
- alert('读取超时,请检查网络连接');
- }
- }
- );
- });
- //$.post提交方式
- /* $("#test_post").click(function(){
- $.post( //该方法接收 参数,url 参数
- 'ajax_test.jsp',
- {
- username:$("#input1").val(),
- userage:$("#input2").val(),
- usersex:$("input3").val(),
- userjob:$("input4").val()
- },
- function(data){ //这个是回调函数
- var myjson;
- eval('myjson='+data+';');
- $("#result").html("姓名="+myjson.username+"<br/>工作"+myjson['userjob']);
- }
- );
- }); */
- });
- </script>
- </head>
- <body>
- <body topmargin="0" marginwidth="0" marginheight="0">
- <div id="result" style="background:orange;border:1px solid red;width=300px;height=600px;"></div>
- <form id="formtest" action="">
- <p><span>姓名</span><input type="text" name="username" id="input1" value=""/></p>
- <p><span>年龄</span><input type="text" name="userage" id="input2" value=""/></p>
- <p><span>性别</span><input type="text" name="usersex" id="input3" value=""/></p>
- <p><span>职业</span><input type="text" name="userjob" id="input4" value=""/></p>
- </form>
- <button id="sedd_ajax">提交</button>
- <button id="test_post">post提交</button>
- <button id="test_get">get提交</button>
- </body>
- </body>
- </html>
- Java页面:
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println(request.getParameter("username"));
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- ArrayList<User> list=new ArrayList<User>();
- User user = new User();
- user.setId(1);
- user.setAge(20);
- user.setName("wkl");
- //转为json对象
- list.add(user);
- //将List转化为JSON
- JSONArray json=JSONArray.fromObject(list);
- System.out.println(json);
- out.write(json.toString());
- out.flush();
- out.close();
- }