ajax java

来源:互联网 发布:站长站源码 编辑:程序博客网 时间:2024/06/13 00:18

之前写分页的时候用了XMLHttp那个方法来写 不好看 这次写的是另外一个 有success的那个写法

index.jsp(可以用.html 只不过顺手点了 才会用jsp)

<html>  <head>    <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">    <meta charset="UTF-8">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->    <script src="js/jquery.min.js"></script>    <script>    function doTchSubmit() {      $.ajax({        type: "POST",        url: "../ajaxtest/test",  //根目录/web.xml里面配的路径      dataType: "text",        cache: false,        data: {            name: $("#name").val(),            pswd: $("#pswd").val()          },        success: function(data) {            var info = document.getElementById("info");          if(data!=null){             var result = eval("(" + data + ")");               info.innerHTML = "名字:"+result.name+" 密码:"+result.pswd;          }else{             info.innerHTML = "添加失败";          }      }      });      }      </script>  </head>  <body>        name:<input type="text" id="name" name="name">        pswd:<input type="text" id="pswd" name="pswd">        <button onclick="doTchSubmit()">btn</button>        <span id="info"></span>  </body></html>

test.java

import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;public class test extends HttpServlet {    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {        System.out.println("get");        doPost(req, resp);    }    protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {        try {            //设置 不要cache 保证中文不乱码            resp.setContentType("text/html");            resp.setHeader("Cache-Control", "no-store");            resp.setHeader("Pragma", "no-cache");            resp.setDateHeader("Expires", 0);            req.setCharacterEncoding("UTF-8");            resp.setContentType("text/html;charset=UTF-8");            PrintWriter out = resp.getWriter();            String name = req.getParameter("name");            String pswd = req.getParameter("pswd");            System.out.println(name+ " , " + pswd);            //用Map来存放数据 然后把map转换成json对象 传递到前台            Map<String, Object> map = new HashMap<String, Object>();            map.put("name", name);            map.put("pswd", pswd);            JSONObject jsonobject=JSONObject.fromObject(map);            out.print(jsonobject);        }catch(Exception e){            e.printStackTrace();        }    }}

web.xml

<servlet>    <servlet-name>Test</servlet-name>    <servlet-class>com.demo.test.test</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>Test</servlet-name>    <url-pattern>/test</url-pattern>  </servlet-mapping>

恩恩 就这样 没什么好说的 要注意的地方就是java那里 用JSONObject返回给前台 这样就方便取值 用map包着只是个人习惯 然后前台ajax那块 基本上就是对着抄就好了

哦哦哦 对了 还要注意一下jar包 分别有:
commons_logging.jar
commons-beanutils.jar
commons-collections.jar
commons-lang.jar
ezmorph-1.0.jar
json-lib.jar

附件:http://download.csdn.net/detail/qq_22778717/9641431

0 0
原创粉丝点击