我的问题是我的程序忽略了else语句。例如,用户没有在字段中输入任何内容并单击提交按钮,else下的语句应该执行。但它没有做它应该做的事情。我被困在这里,无法找到我的代码有什么问题。我试图在Eclipse中使用调试器调试它,它表明它仍然继续使用if语句下的语句,即使某些字段为空。
index.jsp文件:
<body>
<div id="mainContainer">
<form action="processfare.jsp" method="post">
<p>Enter passenger last name: <input type="text" name="lname" size="25"></p>
<p>Enter passenger first name: <input type="text" name="fname" size="25"></p>
<p>Destination:
<select name="dest" size="1">
<option>Quezon Avenue</option>
<option>GMA Kamuning</option>
<option>Cubao</option>
<option>Santolan</option>
<option>Ortigas</option>
<option>Shaw Boulevard</option>
<option>Boni Avenue</option>
<option>Guadalupe</option>
<option>Buendia</option>
<option>Ayala</option>
<option>Magallanes</option>
<option>Taft Avenue</option>
</select></p>
<p>Credit Card Number: <input type="text" name="ccnum" size="25"></p>
<input type="submit" value="Submit">
</form>
</div>
</body>
processfare.jsp文件:
<body>
<%
if (request.getParameter("lname") != null || request.getParameter("lname") != "" &&
request.getParameter("fname") != null || request.getParameter("fname") != "" &&
request.getParameter("dest") != null || request.getParameter("dest") != "" &&
request.getParameter("ccnum").trim().length() > 0 ) {
passBean.setLname(request.getParameter("lname"));
passBean.setFname(request.getParameter("fname"));
passBean.setDestination(request.getParameter("dest"));
passBean.setCCnum(request.getParameter("ccnum"));
if (passBean.luhnTest(request.getParameter("ccnum"))==true){ %>
<% passBean.procStation(); %>
<%PDFDisplay.generatePDF(passBean); %>
<p>The fare amount to <%=passBean.dest %> from North Avenue Station is: P<%=passBean.fare %></p>
<p>Please remember to alight at <%=passBean.stop%></p><br>
<p>LIST OF FARE PROFITS</p>
<p>
TOTAL FARE FOR ALL STOP 1: P<%=passBean.totalFare1 %><br>
TOTAL FARE FOR ALL STOP 2: P<%=passBean.totalFare2 %><br>
TOTAL FARE FOR ALL STOP 3: P<%=passBean.totalFare3 %><br>
TOTAL FARE FOR ALL STOP 4: P<%=passBean.totalFare4 %><br>
TOTAL FARE FOR ALL STOP 5: P<%=passBean.totalFare5 %><br>
TOTAL FARE FOR ALL STOP 6: P<%=passBean.totalFare6 %>
</p>
<form action="index.jsp">
<input type="submit" value="<< GO BACK >>">
</form>
<% } else { %>
<p> Please enter a valid credit card number.</p>
<p>Click <a href = "index.jsp">here</a> to try again.</p>
<% } %>
<% } else {
throw new ServletException ("Invalid input. Please try again");
} %>
</body>
答案 0 :(得分:1)
if (request.getParameter("lname") != null && request.getParameter("lname") != "" &&
request.getParameter("fname") != null && request.getParameter("fname") != "" &&
request.getParameter("dest") != null && request.getParameter("dest") != "" &&
request.getParameter("ccnum").trim().length() > 0 ) {
你的&#34; ||&#34;正在创造一些奇怪的逻辑...
BTW xxx.trim()。length()&gt; 0可能比!=&#34;&#34;
更好答案 1 :(得分:0)
您的所有OR(||)都应为AND(&amp;&amp;)。
if ((request.getParameter("lname") != null) && (request.getParameter("lname") != "") &&
(request.getParameter("fname") != null) && (request.getParameter("fname") != "") &&
(request.getParameter("dest") != null) && (request.getParameter("dest") != "") &&
request.getParameter("ccnum").trim().length() > 0 )
{
***Reset of code follows here
答案 2 :(得分:0)
你可以尝试像这样的if条件
if (request.getParameter("lname") != null && request.getParameter("lname") != "" && request.getParameter("fname") != null && request.getParameter("fname") != "" && request.getParameter("dest") != null && request.getParameter("dest") != "" && request.getParameter("ccnum").trim().length() > 0 ) {
答案 3 :(得分:0)
您也可以为if添加括号:
if ((request.getParameter("lname") != null || request.getParameter("lname") != "") &&
(request.getParameter("fname") != null || request.getParameter("fname") != "") &&
(request.getParameter("dest") != null || request.getParameter("dest") != "") &&
request.getParameter("ccnum").trim().length() > 0 )