jsp里
 <form  id="form1" name="form1" method="get" action="/food_comment/userServlet?operate=search">
    <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce">
             
        <tr>
        <td width="8%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">用户名</span></div></td>
        <td height="20" bgcolor="#FFFFFF" class="STYLE6"><div align="center"><span class="STYLE19"><input type="text" name="username" value=""/></span></div></td>
       </tr>
     
        <tr>
         <td width="10%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">操作</span></div></td>
         <td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center"><input type="submit" value="确定" />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="button" value="放弃" onclick="javascript:if(confirm('确实要放弃吗?'))location='/food_comment/userServlet?operate=giveup'" /></div></td> 
        </tr>
    
    </table>
     </form>  
servlet里 if((req.getParameter("operate")).equals("search"))
{
System.out.println(req.getParameter("operate"));
System.out.println(req.getParameter("username"));
//ArrayList al=um.userquerybyid(req.getParameter("username"));
//HttpSession hs=req.getSession(); 
   // hs.setAttribute("AL",al);
//req.getRequestDispatcher("searchUserCl.jsp").forward(req, res);

}
测试过能用URL后面跟参数传值如/food_comment/userServlet?operate=search,可为什么form中的username却不能用req.getParameter("username")获取。报空指针异常。

解决方案 »

  1.   

    form中的username却不能用req.getParameter("username")获取,这个当然啦
    ,因为 servlet不会读取表单数据改成 method="get" 
    post看看;
    是在不行只能动态指定form1的ation啦+username的值
      

  2.   

    用getParameterMap试试,或者method改成post试试
      

  3.   

    1.name="username" value="" 把value=""去掉
    2.method改成post试试
      

  4.   

    doGet方法处理get方式提交的请求,doPost方法处理post方式提交的请求,service方法两种请求都可以接受。
      

  5.   

    用post是可以的。。能解释下为什么get不行么。我只是粗略了解get,post是否会在地址栏显示这个区别。我的servlet中dopost也是调用的doget方法啊。
      

  6.   

    get方式提交需要在地址栏里面显示传递参数,而post是不需要的
      

  7.   

    zhoupuyue说的service方法两种请求都可以接受我了解。要不然我也不会在servlet中定义dopost()与doget(),我的意思是,为什么我的表单在method="get"的时候servlet获取不到表单里的值,只可以获得url上带的参数,而method="post"时可以,是因为两者所能传递的数据量post>get?所以才用post?