0

I am using the sendRedirect() method. But it doesn't. Please have a look at the following code:-

<%@page import="utility.ConnectionClass,java.sql.* "%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>processadmin</title>
    </head>
    <body>
        <%
          Connection con=null;
          ConnectionClass obj=new ConnectionClass();
          con=obj.createConnection(con);
          String user=request.getParameter("user");
          String pass=request.getParameter("pass");
          String sql="select * from admin where username='"+user+"'";
          Statement stat=con.createStatement();
          ResultSet rs=stat.executeQuery(sql);
          rs.next();
          if((rs.getString(1)==user)&&(rs.getString(2)==pass))
               response.sendRedirect("processadmin.jsp");
          else
            out.println("Not working");
        %>
    </body>
</html>

And when I run this I get the output :- Not Working

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
Vinay Jain
  • 1,653
  • 20
  • 28
  • 1
    You should gradually be [avoiding](http://stackoverflow.com/q/3177733/1037210) this hateful scriptlet which is **highly** discouraged for over a decade. – Lion Jul 01 '13 at 15:59

1 Answers1

2

Compare String using equals() method . == compares String references , not actual contents of the String.

if(user.equals(rs.getString(1)) && pass.equals(rs.getString(2)))

Note:- Please don't use scriptlets in JSP . It is a bad practice. Read this.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • @VinayJain then accept the answer. It might also be better do the code at the top, before any output (like ``) and `return`. Only because of buffering nothing was written yet at the moment of `sendRedirect`. – Joop Eggen Jul 01 '13 at 16:05