-1

how to check whether these two strings are equal in Java using if...else block.

            String passw1=(String)request.getParameter("pass");
            System.out.println("value of passw1"+passw1);
            String passw2=(String)request.getParameter("pass1");
            System.out.println("value of passw1"+passw2);
            //String a=passw1;//String b=passw2;
            if(passw1.equals(passw2))
            {
            out.println("Password matches");
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dharan
  • 303
  • 2
  • 5
  • 14
  • @BalusC I've formerly asked how to use if..else in JSP scriptlet.Later based on answers below I've changed my code which flagged nullpointerexception. So only I had edited this post for further clarification. – dharan Sep 06 '16 at 10:33
  • Next time, ask a new Question per question and do not chameleonize an existing question into an altogether different one. In case you're interested, the question about comparing strings in Java is already answered here: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – BalusC Sep 06 '16 at 10:40
  • 1
    And, writing bad Java code in a JSP file doesn't make it a JSP problem. It's still just a basic Java problem. You'd have had exactly the same problem when writing bad Java code in a normal Java class. In case you have problems with any code outside prehistoric `<% %>` things, but inside JSP tags or EL expressions, then you may talk about a JSP problem. Here's some food for thought: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Sep 06 '16 at 10:43

2 Answers2

2

Scriplet accept java code so its simple like this

    <% 
 if(request.getParameter("pass")!=null && request.getParameter("pass1")!=null){
       String passw1=(String)request.getParameter("pass");
       String passw2=(String)request.getParameter("pass1");
       if(passw1.equals(passw2)){
         //equal
       }
}
   %>

Is it ok or you try to explain other thing?

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
1

Use .equals() method to check equal String, and apply == for values

<%
      String passw1=(String)request.getParameter("pass");
       System.out.println("value of passw1"+passw1); // if null let me know
      String passw2=(String)request.getParameter("pass1");
       System.out.println("value of passw2"+passw2); // if null let me know
      if(passw1.equals(passw2)){ //both string match
              //your logic
             System.out.println("string match");
        }
        else{
             System.out.println("string not match");
        }
%>
yash
  • 2,101
  • 2
  • 23
  • 32
  • using .equals() method displays org.apache.jasper.JasperException: java.lang.NullPointerException error – dharan Sep 06 '16 at 08:16
  • i edited my answer, if any variable is given null value then let me know – yash Sep 06 '16 at 08:19