在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Servlet获取ajax传递的json值

2017-2-7 13:40| 发布者: zhangjf| 查看: 496| 评论: 0

摘要: Servlet获取ajax传递的json值 其实标题可直接写为“记一件愚蠢的事”。另外声明这是只是一篇水文。 原本都用SpringMVC和ajax进行前后台的交互,今天打算试试用原始的Servlet与其进行交互。 起初是打算实现一个跳转( ...
Servlet获取ajax传递的json值

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

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

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

Action如下:

  1. package per.zww.ajax.action;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. @WebServlet("/ServletAjax")
  9. public class ServletAjax extends HttpServlet {
  10. private static final long serialVersionUID = 1L;
  11. public ServletAjax() {
  12. super();
  13. }
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. // TODO Auto-generated method stub
  16. this.doPost(request, response);
  17. }
  18. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  19. request.getRequestDispatcher("hello.jsp").forward(request, response);
  20. }
  21. }
复制代码

JSP页面如下:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. <script src="<%= request.getContextPath() %>/js/jquery-1.7.2.min.js"></script>
  7. <script type="text/javascript">
  8. $(function(){
  9. $("#test").click(function(){
  10. $.post("ServletAjax");
  11. });
  12. })
  13. </script>
  14. </head>
  15. <body>
  16. <input type="button" name="test" id="test" value="测试"/>
  17. </body>
  18. </html>
复制代码

run运行

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

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

下面开始正题

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

Action如下:

  1. package per.zww.ajax.action;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. @WebServlet("/ServletAjax")
  9. public class ServletAjax extends HttpServlet {
  10. private static final long serialVersionUID = 1L;
  11. public ServletAjax() {
  12. super();
  13. }
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. // TODO Auto-generated method stub
  16. this.doPost(request, response);
  17. }
  18. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  19. String username=request.getParameter("username");
  20. System.out.println(username);
  21. request.getRequestDispatcher("hello.jsp").forward(request, response);
  22. }
  23. }
复制代码

JSP页面如下:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Insert title here</title>
  6. <script src="<%= request.getContextPath() %>/js/jquery-1.7.2.min.js"></script>
  7. <script type="text/javascript">
  8. $(function(){
  9. $("#test").click(function(){
  10. $.post("ServletAjax",{username:"zhaoww"},function(data){
  11. window.location.href="ServletAjax";
  12. });
  13. });
  14. })
  15. </script>
  16. </head>
  17. <body>
  18. <input type="button" name="test" id="test" value="测试"/>
  19. </body>
  20. </html>
复制代码

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

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 03:23

Copyright 2015-2025 djqfx

返回顶部