Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if() is not working

 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,i have this servlet:

package userEntryy;

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class viewUser extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String user_nick = request.getParameter("user_nick");
String password = request.getParameter("password");
String idSession = session.getId();
request.setAttribute("user_nick", user_nick);
request.setAttribute("password",password);
request.setAttribute("idSession",idSession);


if ((user_nick == "1") && (password == "1")) {

request.getRequestDispatcher("/viewUser.jsp").forward(request, response);
}
{ request.getRequestDispatcher("/errorUser.jsp").forward(request, response);
}

}


so and
if ((user_nick == "1") && (password == "1"))
isn't working and every time go erroUser.jsp.
when i try user_nick = "1"; password = "1"; it's working.
so what is the deal.

Thank's.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is == the correct operator to use when comparing Strings?
 
Nacho Espinosa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Syskata:

The problem here is that the operator == for a String is incorrect, In your code you use == but it must be equals() method.

The correct code is the next:


if (user_nick.equals("1") && password.equals("1) {
//true
} else {
//false
}


See you

Nacho Espinosa
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now working.
thank you very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic