1. 程式人生 > >servlet如何接收Ajax傳來的值,ajax傳值給servlet並且解決返回值為空的現象

servlet如何接收Ajax傳來的值,ajax傳值給servlet並且解決返回值為空的現象

首先我的程式碼:


$(document).ready(function(){
	$('#button').click(function(){
        $.post(
            'Servlet',
            {
        		name:$("#name").val(),
        		password:$("#password").val()
            },
            function(data,status) {
               if(status=="success")
            	   {
            	   		var i=5;
            	   		setInterval(function(){
            	   			alert("歡迎"+data.name+"成功註冊,"+i--+"秒後進入登陸介面");			
            	   		},1000);
            	   		window.location.href="index.html";
            	   }
            }
        )
    });
});

這是正確之後傳值成功的版本,之前錯誤的版本是要傳資訊寫成一個var dat= new Array(); 然後給這個陣列賦值,發現傳是傳過去了,但是servlet根本沒有獲取到我的值,很生氣 試了好久,發現這種傳陣列的方式好像是不對的,所以改成了上圖,傳值成功了。

package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import Bean.Bean;
import Sql.Sql;
/**
 * Servlet implementation class Servlet
 */
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Servlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		Bean bean=new Bean();
		String name = request.getParameter("name");
		String password =request.getParameter("password");
		System.out.println(name);
		System.out.println(password);
		bean.setName(name);
		bean.setPassword(password);
		Sql sql=new Sql();
		boolean a=sql.select(bean);
		if(a==true)
		{
			out.println("{name:'"+name+"',password:'"+password+"'}");
		}
		else {			
		}
//		response.getWriter().append("Served at: ").append(request.getContextPath());
//		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			PrintWriter out=response.getWriter();
			Bean bean=new Bean();
			String name = request.getParameter("name");
			String password =request.getParameter("password");
			bean.setName(name);
			bean.setPassword(password);
			Sql sql=new Sql();
			sql.insert(bean);
			out.println("{name:'"+name+"',password:'"+password+"'}");
		}catch(Exception e) {
			e.printStackTrace();
		}
		doGet(request, response);
	}

}

如何接收到值如上圖的方式

String name = request.getParameter("name");
        String password =request.getParameter("password");

而且你一定要寫這一部分

out.println("{name:'"+name+"',password:'"+password+"'}");

這一部分屬於給Ajax返回值的部分不然的話,會出現前端Ajax錯誤,到時候前端咋找也找不到錯,其實是後端的原因。後續我會把這整個專案貼上來。