【问题标题】:null return on getParameter text field in jsp page running on tomcat 5.x在tomcat 5.x上运行的jsp页面中getParameter文本字段返回null
【发布时间】:2012-12-18 07:12:41
【问题描述】:

我收到一个奇怪的页面错误,页面在每次提交表单时在 nullpointerexception 和实际执行之间不断波动。我正在编写一个简单的 jsp 页面并在 tomcat 5.x 服务器上运行它。我正在从表单的文本字段中获取数据并在表单提交时对其进行处理。这是我正在尝试的代码:

form id="mainForm" method=POST action="index.jsp">

<input type="text" name="reportName" size=20> 



<a class="btnBlue btnBlueRight" href="#" onclick="document.getElementById('mainForm').submit();" > 

            <% 
            if(request.getParameter("reportName")!=null){
            //do things
            else
            //print message
            %>

对于此代码,地址栏中的页面地址是 localhost:8080/# 或 localhost:8080/index.jsp 用于每个表单提交。我是 jsp 新手,所以无法解决这个问题。

【问题讨论】:

  • 请包含 npe(空指针异常)。
  • 我处理了异常,所以它只显示 java.lang.nullpointerexception
  • request.getParameter("reportName") 发生在页面构建时,它可能是用于构建页面的提交/查询未发送参数。例如,如果直接调用jsp,则除非在url中指定,否则将没有参数。
  • 我明白输入文本是问题所在,不知何故它没有为第一次提交表单初始化,但如果我重新提交表单,它会很好地工作!
  • 显示的代码中没有 npe。异常将具有发生 npe 的行号。如果您在 //do thingsprint message 部分中有代码,则可以将其包含在内,并且有人可以为您找到您的 NPE。

标签: java javascript forms jsp tomcat5.5


【解决方案1】:

这是因为reportName没有设置任何第一次页面加载的地方!例如尝试导航

localhost:8080/index.jsp?reportName=all

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-10
    • 2020-10-23
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2019-05-21
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多