服务器接受链接地址(get方式)输入的数据,需要对数据进行鉴别
- 首先要规避空指针异常
 
package com.wenyue.request;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import javax.print.DocFlavor.STRING;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestDemo3 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
//      http://localhost:8080/day06/servlet/RequestDemo3?name=flx
        String value = request.getParameter("name");
        System.out.println(value);
        
        //http://localhost:8080/day06/servlet/RequestDemo3?like=sing&like=dance
        String likes[] = request.getParameterValues("like");
//      if(likes != null){ 
//          for(String like : likes){
//              System.out.println(like);
//          }
//      }
        //这种循环比上面的要好
        //代码量少,规避了空指针异常,又取出了数组中的数据
        //开发中尽量使用这个,这种获取数组数据的方法在开发中使用的特别多,重要
        for(int i=0;likes != null && i<likes.length;i++){
            System.out.println(likes[i]);
        }
        
        System.out.println("----------------------------");
        
        //获取所有名称,并根据名称获取值
        Enumeration<String> e = request.getParameterNames();
        while(e.hasMoreElements()){
            String name = (String) e.nextElement();
            value = request.getParameter(name);
            System.out.println(name + "=" + value);
        }
        
        System.out.println("-------------------------");
        
        //框架中经常使用
        //name=flx&name=xxx
        Map<String, String[]> map = request.getParameterMap();
        for(Entry<String, String[]> me : map.entrySet()){
            String name = me.getKey();
            String[] v = (String[]) me.getValue();
            System.out.println(name + "=" + v[0]);
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
 
}
从数组中取数据的一种方法
        //代码量少,规避了空指针异常,又取出了数组中的数据
        //开发中尽量使用这个,这种获取数组数据的方法在开发中使用的特别多,重要
        for(int i=0;likes != null && i<likes.length;i++){
            System.out.println(likes[i]);
        }
    }
