求知若渴,谦卑若愚

少说话,多做事,很难
posts - 3, comments - 2, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

request.getParameter("txtValue") 头疼的很

Posted on 2010-08-07 15:51 清流鱼 阅读(491) 评论(1)  编辑  收藏 所属分类: jsp+servlet

一个jsp页面,有一个文本框:

<input type="text" name="txtValue" maxlength="120px"/>
通过java code来取值,并去掉输入为空格的可能性:
String txtValue=request.getParameter("txtValue").trim();
我没有向文本框输入任何值,可是却出现异常:
 1org.apache.jasper.JasperException: An exception occurred processing JSP page /SelDelTask.jsp at line 66
 2
 363:     pageDivide.setRows(10);
 464%>
 565<%
 666:     String txtValue=request.getParameter("txtValue").trim();
 767:     String selectValue=request.getParameter("select");
 868:     int resultCounts=0;
 969:     int totalPages=0;
10
调试了好久,才知道原因,当我们没有向文本框中输入时值时,
String txtValue=request.getParameter("txtValue")
就相当于
String txtValue=null;
txtValue为空时,就是只声明,未赋值,自然就不能用trim()方法,否则会抛出空指针异常。如果我们想判断文本框中是否输入值及输入值是否为空格时,可以这样写(请注意条件中的先后顺序):
if(txtValue==null||txtValue.trim()=="")
这样就不会出现错误了,希望对大家有所帮助!

评论

# re: request.getParameter("txtValue") 头疼的很  回复  更多评论   

2016-04-16 00:49 by 洒水车
sac

只有注册用户登录后才能发表评论。


网站导航: