<form action="redirect1.jsp" method="post" name="form">
   <input type="text" name="boy">
   <input type="submit" value="submit">
  </form>redirect1.jsp页面:
 <%
   String str=request.getParameter("boy");
   if(str==null)
   {
   str="";
   }  
   byte b[]=str.getBytes("ISO-8859-1");
   str=new String(b);
   if(str.equals(""))
   {
   response.sendRedirect("date.jsp");
   }
   else
   {
   out.print("welcome");
   }
  
   %>我想问的是:
为什么要加这一段:if(str==null)
   {
   str="";
   }直接写成这样为什么不可以?: <%
   String str=request.getParameter("boy");
    
   byte b[]=str.getBytes("ISO-8859-1");
   str=new String(b);
   if(str==null)
   {
   response.sendRedirect("date.jsp");
   }
   else
   {
   out.print("welcome");
   }
  
   %>困惑中

解决方案 »

  1.   

    ="" 和 null 有相似的地方:用戶沒有輸入。你的想法中,漏了。
      

  2.   

    如果像你那样写,一旦str==null 那么 你的这一句byte   b[]=str.getBytes("ISO-8859-1");就会报空指针的错误,因为只有str不是空指针的时候 才能应用str.getBytes()
      

  3.   

    null和""并不是一个意思.null是str这个对象更本不存在.而""是用户没有输入,拿到了空值.--------------------------------
     if(str==null) 
        { 
        str=""; 
        } 
    这段的意思就是相当, str这个对象不存在,就当做用户没有输入信息.--------------------------------
     <% 
        String   str=request.getParameter("boy"); 
          
        byte   b[]=str.getBytes("ISO-8859-1"); 
        str=new   String(b); 
        if(str==null) 
        { 
        response.sendRedirect("date.jsp"); 
        }     else 
        { 
        out.print("welcome"); 
        } 
     %> 
    这样的话,当用户没有输入信息的话,你会打出welcome的字样.和上面代码转到date.jsp的效果也不一样啊~以上代表个人看法,希望高手指点.
      

  4.   

    如果str==null 则执行byte   b[]=str.getBytes("ISO-8859-1");  会报NullPointException
      

  5.   

    如果不要,哪么byte       b[]=str.getBytes("ISO-8859-1");   就会报错