0

I wrote the below code to test the status of msgCode. If the msgCode is not Success it should redirect to error.jsp file. If it is a Success it should stay on the same page . When I ran the code the page always redirects to error.jsp although msgCode is Success. What mistake did I do in my code. Can you please help me if you can. Thank in advance.

 <%@ page import="com.siebel.SurveyWebService.SurveyTester" %>
<%  

   SurveyTester tc = new SurveyTester();
   tc.getResult();
   java.lang.String msgCode = tc.getResult2().getStatusCode(); 

  %>
 <%= msgCode%>
 <%
 if (msgCode.toString() != "Success")
 {    
    response.sendRedirect("error.jsp");
  }
%>
  • 2
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – BackSlash Feb 03 '17 at 21:33

1 Answers1

0

First of all, you should use equal method for string comparison. Secondly, even though it's not a problem at the moment but you are creating objects in your jsp and faking the response. tc.getResult2().getStatusCode() is not an actual HTTP response from server.

hhafeez
  • 73
  • 9
  • Faking the response? How? The OP is just printing a number he got from a method, this isn't faking the response at all. – BackSlash Feb 03 '17 at 21:40
  • he should not do that if he really want to see the actual response from server. The actual response code are generated by server and should not be faked with such methods. – hhafeez Feb 03 '17 at 21:47
  • You are assuming the msgCode is the server code: the OP never said that. And it's not faking anything, since the OP is not faking the http response but just printing a number (and the http code is still 200 if the response is valid, no matter what number he prints). You can print everything you want on a webpage, and printing a number does not ever fake a response. – BackSlash Feb 03 '17 at 21:53
  • I am not assuming. I am talking abut conventions and practices and how to do things in a correct way. yes! you can print any number you want by disregarding the actual response but if you want to make decision on the basis of http response code (which OP is pretending to do ) then you will not mock your responses like this method. – hhafeez Feb 03 '17 at 22:05
  • The OP is not pretending to make a decision on the basis of http response code. Read the code carefully. `msgCode` is a string that gets returned by a method he calls. It is **not** the http code. And I'll say it another time: Since the OP is **not** editing the real response code, he is **not** mocking anything. A mock is a situation where you change the actual data to some fake data: to mock a response you need to change the response itself (code & message). The OP is just printing a string, he is not faking/mocking the response. – BackSlash Feb 03 '17 at 22:09