ajax传值后台获取初步了解

前台页面代码如下: 

   <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%  
        String path = request.getContextPath();  
        String basePath = request.getScheme() + "://"  
                + request.getServerName() + ":" + request.getServerPort()  
                + path + "/";  
    %>  
      
      
      
      
      
      
    My JSP 'index.jsp' starting page  
      
      
      
      
      
      
      
    
      
      
      
      
        
用户名:
密码:
后台代码如下
response.setContentType("text/html");  
        response.setCharacterEncoding("utf-8");  
        PrintWriter out = response.getWriter();  
        /** 
         * 接受从前端传递过来的json数据,要注意引入解析json的包 
         */  
        String name=request.getParameter("ss");
        System.out.println(name);
        JSONObject json=JSONObject.fromObject(name);  
        Iterator iter = json.keySet().iterator();    
           Map map = new HashMap();    
           /** 
            * 解析来自前端页面的值,解析json数组 
            */  
           while (iter.hasNext()) {    
            String key = (String) iter.next();    
            String value = json.getString(key);    
            System.out.println("key:"+key+",value:"+value);  
            map.put(key, value);    
           }  
           Map jmap = com.alibaba.fastjson.JSON.parseObject(name);
           Map mjmap = new HashMap();

           for(Object obj:jmap.keySet()){
        	   mjmap.put(obj, jmap.get(obj));
           }
           System.out.println(mjmap);
           

          
        /** 
         * 前端页面设置了dataType为json格式,则后台返回时返回的格式必须为json格式 
         */  
           out.print("提交成功");  
        out.flush();  
        out.close();
	}

更详细参见:web中用ajax传递json数据到后台



你可能感兴趣的:(ajax传值后台获取初步了解)