servlet向ajax傳遞list數據類型,ajax向servlet傳遞array數據類型

因工做須要,javascript

1,後臺向前臺傳遞一個listhtml

2,前臺向後臺傳遞相似於list的結構,可是由於javascript不支持list類型,因此只能使用二維數組代替java

後臺運行後的截圖:                                                                前臺運行後的截圖:               jquery

                                                   

後臺代碼(TestJson.java)web

package com.zm.servlet;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.ArrayList;  
import java.util.HashMap;
import java.util.List;  
import java.util.Map;
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import net.sf.json.JSONArray;  
import net.sf.json.JSONObject;
  
public class TestJson extends HttpServlet {  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
           doPost(request,response);  
    }  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        
        System.out.println("into doPost");
        
        response.setContentType("text/html");     
        String sName= request.getParameter("name");//獲得ajax傳遞過來的paramater  
        System.out.println(sName);
        
        //String sTicketId= request.getParameter("ticketId");//獲得ajax傳遞過來的paramater  
        //System.out.println(sTicketId);

        try {  
            String[] sTicketIds = request.getParameterValues("ticketId[]");        
            if(sTicketIds !=null){  
                for(String sTicketId:sTicketIds){  
                    System.out.println(sTicketId);  
                }  
            }else{  
                System.out.println("獲取數組參數失敗!");  
            }
        } catch (Exception e) {  
            e.printStackTrace();  
        }  

        PrintWriter printWriter = response.getWriter();  
        List list = new ArrayList();//傳遞List  
        
        Map m=new HashMap();//傳遞Map      
        User u1=new User();  
        u1.setUsername("zah");  
        u1.setPassword("123");  
        User u2=new User();  
        u2.setUsername("ztf");  
        u2.setPassword("456");    
        list.add(u1); //添加User對象        
        list.add(u2);     //添加User對象    
        
        m.put("u1", u1);  
        m.put("u2", u2);     
          
        JSONArray jsonArray2 = JSONArray.fromObject( list );  
        //把java數組轉化成轉化成json對象   
        //JSONObject jsonObject =JSONObject.fromObject(m);//轉化Map對象  
        printWriter.print(jsonArray2);//返給ajax請求  
        //printWriter.print(jsonObject);//返給ajax請求  
        printWriter.flush();
        printWriter.close();
        System.out.println("finish");
    }  
}    

 

前臺代碼(first.jsp)ajax

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>   
    <script type="text/javascript" src="jQuery/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">  
       function test2(){  
          var ticketId = new Array();
          for(var i = 0; i < 5; i++) {
              ticketId.push(i);
          }
           $.ajax({
                type:"POST", //請求方式  
                url:"./testJson", //請求路徑  
                cache: false,     
                data:{//傳參  
                    "name":"zhang3",
                    "ticketId":ticketId,    
                },
                dataType: 'json',   //返回值類型  
                success:function(json){        
                    alert(json[0].username+" " + json[0].password);    //彈出返回過來的List對象  
                    alert(json[1].username+" " + json[1].password);    //彈出返回過來的List對象
                }  
            });  
      }  
    </script>  
  </head>  
    
  <body>  
    <input type="button" name="b" value="TestJson" onclick="test2()"/>
    <input type="button" value="download file" onclick="DownURL('333.js','d:/555.js')"/>
  </body>  

</html>

web.xml
json

    <!-- 測試jquery json-->
    <servlet>  
    <servlet-name>testJson</servlet-name>  
    <servlet-class>com.zm.servlet.TestJson</servlet-class>  
  </servlet>  
   <servlet-mapping>  
    <servlet-name>testJson</servlet-name>  
    <url-pattern>/testJson</url-pattern>  
  </servlet-mapping>  
相關文章
相關標籤/搜索