mercredi 8 avril 2015

If else statement logic error

My problem is that my program ignores the else statement. For example, a user didn't entered anything in the field and clicked on the submit button, statements under else should execute. But it doesn't do what it is supposed to do. I'm stuck in here and can't find what's wrong with my code. I've tried to debug it using the debugger in Eclipse, it shows that it still continue to the statements under the if statement even though some of the fields are null.


index.jsp file:



<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 file:



<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>

Aucun commentaire:

Enregistrer un commentaire