0

can anyone please help me with this? the first if statement works completely fine but the second if is never true meaning that whenever i print 'not' it is fine but when is print 'ok' it's not working and obviously if i use else with the first if then too it's not working

<script type="text/javascript">
    function signupfunction(){
        var name = document.getElementById('signupname').value;
        var email = document.getElementById('signupemail').value;
        var password = document.getElementById('signuppassword').value; 
        $.ajax({
            url : "AdminSignup?name=" + name + "&email=" + email + 
"&password=" + password + "",
            type : "GET",
            async : true,
            success : function(data) {
                if(data == 'not'){
                    alert("Email already exists");
                } 
                if(data == 'ok'){
                    alert('hello');
                }
            }
        });
    }
</script>

here is my doGet() function of servlet

protected void doGet(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost/amazon? 
                user=root&password=test123");
            PreparedStatement statement = con.prepareStatement("select * from 
                sellers where email=?");
            statement.setString(1, request.getParameter("email"));
            ResultSet rs = statement.executeQuery();
            if(rs.next()) {
                response.getWriter().print("not");
            } else {
                PreparedStatement statement1 = con.prepareStatement("insert 
                    into sellers(email,password,name) values(?,?,?)");
                statement1.setString(1, request.getParameter("email"));
                statement1.setString(2, request.getParameter("password"));
                statement1.setString(3, request.getParameter("name"));
                statement1.executeUpdate();
                session.setAttribute("sessionAdminEmail", 
                    request.getParameter("email"));
                session.setAttribute("sessionAdminName", 
                    request.getParameter("name"));
                response.getWriter().print("ok");
            }
        } catch (Exception e) {

          }
    }
Shivam Bhasin
  • 100
  • 1
  • 8

1 Answers1

0

You are mixing up

response.getWriter().print("ok"); //this will not send your value to ajax

with:

response.getWriter().write("inserted new user");   //this will

Your Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws ServletException, IOException {
            HttpSession session = request.getSession();
            response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
            response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
 try {


        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/amazon?user=root&password=test123");

        PreparedStatement statement = con.prepareStatement("select * from sellers where email=?");
        statement.setString(1, request.getParameter("email"));
        ResultSet rs = statement.executeQuery();

        if(rs.next()) {
            //if the email exists in sellers, then this will always evaluate to true...
            //response.getWriter().print("not"); //print will not send your value back to ajax..
            response.getWriter().write("email exists");       // Write response body.
        } else {
            PreparedStatement statement1 = con.prepareStatement("insert into sellers(email,password,name) values(?,?,?)");
            statement1.setString(1, request.getParameter("email"));
            statement1.setString(2, request.getParameter("password"));
            statement1.setString(3, request.getParameter("name"));
            statement1.executeUpdate();
            session.setAttribute("sessionAdminEmail", request.getParameter("email"));
            session.setAttribute("sessionAdminName", request.getParameter("name"));
            //response.getWriter().print("ok"); print will not send your value back to ajax
            response.getWriter().write("inserted new user");       // Write response body.
        }
    } catch (Exception e) {

      }
}

Also worth mentioning that == is not the same as ===. Use === in this case, it is the comparison operator you're looking for. Check out this for an explanation about the difference

<script type="text/javascript">
    function signupfunction(){
        var name = document.getElementById('signupname').value;
        var email = document.getElementById('signupemail').value;
        var password = document.getElementById('signuppassword').value; 
        $.ajax({
            url : "AdminSignup?name=" + name + "&email=" + email + 
"&password=" + password + "",
            type : "GET",
            async : true,
            success : function(data) {
                if(data === 'email exists'){
                    alert("Email already exists");
                } 
                if(data === 'inserted new user'){
                    alert('new user');
                }
            }
        });
    }
</script>

Check out this answer also, it gives a great explanation on how to use Ajax with Servlets. (much easier than the way you're currently doing it)

How to use Servlets and Ajax?

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • Did the above answer help? I just had (adding my weight here) `===` in mind for comparison. – Shreyas Jun 12 '18 at 21:23
  • No it is still the same. The first if condition is working but the second one is not working. I have now used write instead of print in servlet and in javascript i have tried comparing the stings using '==' and '===', the second if is not working in both conditions – Shivam Bhasin Jun 13 '18 at 07:32
  • Do a `console.log (data)` (or alert) before the if statements but inside the success block... and tell me what you see – Jonathan Laliberte Jun 13 '18 at 10:16