Servlet獲取ajax傳遞的json值

jopen 8年前發布 | 18K 次閱讀 Servlet Ajax JSON Java開發

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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!