• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet if-else condition not working  RSS feed

 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys i have this code no matter what i do the control doesn't go the else part of the condition also tried with else if also with no luck...

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.*;
/**
* Servlet implementation class result
*/
public class result extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public result() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String number = request.getParameter("search1");
System.out.println(number);
String connectionparams="jdbc:mysql://localhost:3306/fees";
String db="fees";
String uname="root";
String psword="password";
// Declaring classes required for Database support
Connection connection=null;
try { // Loading the available driver for a Database communication
Class.forName("com.mysql.jdbc.Driver");
//Creating a connection to the required database
connection = DriverManager.getConnection (connectionparams, uname, psword);
//Add the data into the database
String sql = "select * from fee1 where id=?";
PreparedStatement prep = connection.prepareStatement(sql);
prep.setString(1,number);
ResultSet result = prep.executeQuery();
while(result.next())
{
if(number.equals(result.getString(5)))
{
System.out.println("IF condition");
String name = result.getString(1);
String fee = result.getString(2);
String Course = result.getString(3);
String occupation = result.getString(4);
String id = result.getString(5);
String balance = result.getString(6);
String date = result.getString(7);
System.out.println(name + "\t" + fee + "\t" + Course +"\t"+ occupation + "\t" + id + "\t" + date);
System.out.println("\t" + balance +"\t");
HttpSession sess= request.getSession(true);
request.getSession().setAttribute("name",name);
request.getSession().setAttribute("fee",fee);
request.getSession().setAttribute("Course",Course);
request.getSession().setAttribute("occupation",occupation);
request.getSession().setAttribute("id",id);
request.getSession().setAttribute("balance",date);
request.getSession().setAttribute("balance",balance);
String web="http://localhost:8080/f1/w1r.jsp";
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", web);
RequestDispatcher view = getServletContext().getRequestDispatcher("/w1r.jsp");
view.forward(request,response);
}
else
{
HttpSession sess= request.getSession(true);
String web="http://localhost:8080/f1/Index.jsp";
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", web);
RequestDispatcher view = getServletContext().getRequestDispatcher("/fail.jsp");
}
}
}
catch(Exception E)
{
//Any Exceptions will be caught here
System.out.println("The error is:"+E.getMessage());
}

}
}

 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you print both values before the if condition.


And if you are expecting a single row in the ResultSet, use if condition instead of while loop.
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i will try that and revert back

update...

tried but no luck
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what output did you get when you printed out the values you are testing?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by no luck?
If you are printing values after the start of while loop and before the start of if condition and you are not getting any output,
Then the select query is not returning any record.
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when using valid values i get the desired output but when using invalid values i just a white screen
i checked the url it is
http://localhost:8080/f1/result?search1=1
that's it nothing else
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If there are no results then the while loop is never entered.
So the if/else is never entered and you simply exit the method (hence a white screen).

I suspect you actually want:
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem Solved but ....
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:

If there are no results then the while loop is never entered.
So the if/else is never entered and you simply exit the method (hence a white screen).

I suspect you actually want:



could you please explain me a bit more as to why the previous code didn't work?the control went to if block and executed ..but it never went to else part.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the solution was?
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Dave..thank for all other guys who helped
Your idea replaced the while with if....would you care to explain again as of why my code didn't work?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The control never went to the 'if/else' block if there were no results, as the while loop was never entered (result.hasNext() was false).
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but then i was able to see the output of if-block when correct number as entered
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because when the correct number was entered you got a result back from the database, so results.hasNext() returned true, so the while loop was entered.
 
Aditya Mukundan
Greenhorn
Posts: 17
Eclipse IDE Linux Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thank you Dave again ....and other guys as well ....have a nice day problem solved
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!