1

How can I access isValid in this class from a JSP?

public class bController extends aController {    
    public static boolean isValid;
    private void empData(empmonthly data){
    while (empIterator.hasNext()){              
          isValid = false; 
          data1 = (Entity)empIterator.next();              
          empNumber = data1.getInt("NO_EMP");              
          if (empNumber > 0){ 
              try{
                  cStatus= data.getString("Status");  
                  if (cStatus.equals("Temp")){
                      isValid = true;  
                  } else {
                      isValid = false; 
                  }
              }
              catch(Exception e){
                  cStatus = "";                   
              }   
              loadEmpD();
          }
     }
}
2
  • It is not a global variable, it is a static field. The practice of using "global variables" is generally discouraged. Static fields that are not constants should generally only serve the class where they exist, and you should pass information directly to other objects using parameter to methods. Commented Mar 13, 2015 at 13:46
  • I clarified the title to make it more accurate and get to the point faster, then removed an entirely redundant problem statement from the body to save a little time, along with thanks (which we don't use here). I also made sure the remaining body starts with the main point, fixed a few glitches in the code indentation, and added a relevant tag for better visibility. Commented Mar 13, 2015 at 18:51

1 Answer 1

1

You access static fields the very same way from JSP and from Java, so you simply need:

<p>isValid: <%=bController.isValid%></p>

BUT this is strongly discouraged way of accessing fields (unless they are final). And note, that servlet that is made of your JSP may be working in several threads, thus value stored in that field may be invalid for other threads.

Sign up to request clarification or add additional context in comments.

1 Comment

Not regarding to your question, but it is much safer to write "Temp".equals(cStatus) than what you actually did (find out why) and in fact you can simply write this line isValid = "Temp".equals(cStatus) what saves you typing and is much more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.