码迷,mamicode.com
首页 > Web开发 > 详细

Servlet获取ajax传递的json值

时间:2016-01-08 23:36:50      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

Servlet获取ajax传递的json值

标签:

原文地址:http://www.cnblogs.com/zhaoww/p/5115134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!