Servlet获取ajax传递的json值

jopen 8年前

Servlet获取ajax传递的json值

其实标题可直接写为“记一件愚蠢的事”。另外声明这是只是一篇水文。

原本都用SpringMVC和ajax进行前后台的交互,今天打算试试用原始的Servlet与其进行交互。

起初是打算实现一个跳转(虽然感觉没什么意义):

Action如下:

package per.zww.ajax.action;    import java.io.IOException;  import javax.servlet.ServletException;  import javax.servlet.annotation.WebServlet;  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  @WebServlet("/ServletAjax")  public class ServletAjax extends HttpServlet {      private static final long serialVersionUID = 1L;               public ServletAjax() {          super();      }      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          // TODO Auto-generated method stub          this.doPost(request, response);      }      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          request.getRequestDispatcher("hello.jsp").forward(request, response);      }    }

JSP页面如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <title>Insert title here</title>  <script src="<%= request.getContextPath() %>/js/jquery-1.7.2.min.js"></script>  <script type="text/javascript">  $(function(){      $("#test").click(function(){          $.post("ServletAjax");      });  })  </script>  </head>  <body>      <input type="button" name="test"  id="test" value="测试"/>  </body>  </html>

run运行

坑爹了,它怎么不跳转呢?! 用firebug查看,没错啊200OK。怎么不跳转?!

坑爹了,好久没碰连ajax使用都忘了。url是发送请求的地址,若是想要想跳转需在function回调函数里面写上window.location.href="ServletAjax";

下面开始正题

其实Servlet获取ajax传递的json值很容易,只需在servlet用request.getParameter()方法即可。

Action如下:

package per.zww.ajax.action;    import java.io.IOException;  import javax.servlet.ServletException;  import javax.servlet.annotation.WebServlet;  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  @WebServlet("/ServletAjax")  public class ServletAjax extends HttpServlet {      private static final long serialVersionUID = 1L;               public ServletAjax() {          super();      }      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          // TODO Auto-generated method stub          this.doPost(request, response);      }      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          String username=request.getParameter("username");          System.out.println(username);          request.getRequestDispatcher("hello.jsp").forward(request, response);      }    }

JSP页面如下:

<%@ page language="java" contentType="text/html; charset=utf-8"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <title>Insert title here</title>  <script src="<%= request.getContextPath() %>/js/jquery-1.7.2.min.js"></script>  <script type="text/javascript">  $(function(){      $("#test").click(function(){          $.post("ServletAjax",{username:"zhaoww"},function(data){              window.location.href="ServletAjax";          });      });  })  </script>  </head>  <body>      <input type="button" name="test"  id="test" value="测试"/>  </body>  </html>

来自: http://www.cnblogs.com/zhaoww/p/5115134.html