File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes if() is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if() is not working" Watch "if() is not working" New topic

if() is not working

Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
hi,i have this servlet:

package userEntryy;

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

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.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62269

Is == the correct operator to use when comparing Strings?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nacho Espinosa
Ranch Hand

Joined: Jan 17, 2007
Posts: 30
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) {
} else {

See you

Nacho Espinosa

Greetings<br />Nacho Espinosa<br />SCJP 1.4
Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
now working.
thank you very much.
I agree. Here's the link:
subject: if() is not working