2013年9月20日星期五

Database draw strings and string passed the same jsp form , equals () function does not match

According to accounts from the database to get the password and submit the form from jsp password printed in the console is the same , but why if statement password.equals (password1) can not enforce it , if the parameters replaced if true, if the statement can be performed quickly , which is why ?
Here is the servlet code :

public class checkServlet extends HttpServlet {


public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

GuestDao guestDao = new GuestDao ();
String password1 = (String) request.getParameter ("password");
String account = (String) request.getParameter ("account");

System.out.println (" incoming password is :" + password1);

try {
String password = guestDao.checkGuest (account); / / checkGuest () returns a query from the database string
System.out.println (" get under the account password is :" + password);
if (password.equals (password1))
{
System.out.println (" password matches success" ) ; ; / / console does not output
/ / ServletContext application = this.getServletContext ();
/ / RequestDispatcher rd = application.getRequestDispatcher ("/ mainPage.jsp");
/ / rd.forward (request, response);
}
} catch (Exception e) {
e.printStackTrace ();
}

}
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost (request, response);
}
}

------ Solution ------------------------------------ --------
see what both have spaces in the string , each string is called . trim function to try
------ Solution -------- ------------------------------------
carefully confirm the next
see if there are spaces or
------ For reference only -------------------------- -------------


not but write:
boolean real = password.equals (password1);
if (real)
{
/ / code
}

you can , do not know why

------ For reference only ---------------------------------- -----


boolean real = password.equals (password1);
if (real)
{
/ / code
}

can
------ For reference only ------------------------------ ---------

  
not but write:   
boolean real = password.equals (password1);   
if (real)   
{   
/ / code   
}   
  
you can , do not know why   
 


can, thank you

没有评论:

发表评论