Link to home
Start Free TrialLog in
Avatar of clo1
clo1

asked on

Global Variable

   I was trying to run the servlet in the debugger, however, the Visual Cafe prompt me for this:
 
Unable to load sun.servlet.http.HttpServer.class for debugging
 
so I cannot trace the variable life. Is there any other way to debug a servlet?


ALSO, I have declared a global variable, but I cannot retrieve the value since I have assigned it.

And here is the code sample:
 
 
public class stockDB extends java.lang.Object{
    .....
    .....
    .....
    private boolean showSPRC;
   
   
    public String[] login(String inUsr_id, String inUsr_pwd, String inUsr_IP) throws eclException{
            ...............................
            if (rs.next()){
                dbUser_pwd = (rs.getString(1)).trim();
                returnResult[1] = rs.getString(2);
                returnResult[2] = rs.getString(3);
                returnResult[3] = rs.getString(4);
               
                //Trigger the show sale price flag
                if (rs.getString(5).equals("1"))
                    showSPRC = true;                            <---------------------------------------------------------------(ASSIGN FROM HERE)
            }
           
    public String online_stk_info_filtered(String inSupp_code, int nextStart, String sortType) throws eclException{
                ..................................
                else{
                    // NON PAGINATION ALOGRITHM
                    for (int i = 0; i < stockVector.size(); i++){
                        result.append("<row>");
                               
                        Vector subVector = (Vector)stockVector.elementAt(i);
                        result.append("<col>").append(((String)subVector.elementAt(0)).trim()).append("</col>");
                        result.append("<col>").append(((String)subVector.elementAt(1)).trim()).append("</col>");
                       
                        QTY = (String)subVector.elementAt(2);
                       
                        result.append("<col>").append(QTY).append("</col>");
                       
                        if (showSPRC)                            <-------------------------------------------------------------------------(RETRIEVE THE VALUE FROM HERE)
                            result.append("<sprc>").append((String)subVector.elementAt(3)).append("</sprc>");
                           
                        result.append("</row>");
                    }
                }
            }
Avatar of exorcist
exorcist

may I know how you managed to declare a GLOBAL variable?
Avatar of clo1

ASKER

I have post it on the above sample:

private boolean showSPRC;
ASKER CERTIFIED SOLUTION
Avatar of dnoelpp
dnoelpp
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is not a global variable but an instance variable.
Do you initialize this variable upon construction?

and by the way: there are some informal agreements on style you should consider. They are not necessary and might look stupid at the moment, but it will make your code more readable and fitting better into the other java libraries.
1. class Names should start with a capital letter.
2. dont use underscore "_" in compound words. Just concat the words and start each word with a capital letter.
I also had to debug servlets. First I just used System.out.println and System.err.println.
The output of at least one of these I found in the logfiles of the servlet engine or
webserver.
The second version was to write some log statements to a file, but this file is still on
the web server and I always had to search it.
Then I wrote a little debug tool. A class which I use in the servlet which opens a socket
connection to a specified host and port and which sends all debug output over the
connection. And a little application which listens for incoming connections, opens a new
window when it got a connection and displays all the log statements sent over the socket.
Hi Elpp
Did u use jdpa or jdb.
what is the comment on that

And log files are the best way to debug the servlet in my personal opinion.

And by declaring it as static . u will end up in probs u have call within an static method and lot more.

By the by clo declaring a variable in the class is nothing but an attribute to a class it is not global. Their is no global variable as such in java. U have to have it in some class. If it is in some class then it is a attribute.

Any comments

Cheers
shyam
Neither, I seldom use the debugger. Most of the time I experiment with the BeanShell if I am not sure about something, and when it's working and it was important and difficult, I write a test case to it, so in future I can stay sure the thing still works when I modified it.

Yes, I think, standard output or log files work the best for servlets.

As for "attributes", you mean member fields. Be careful with terminology. A member is everything inside a class, say fields, methods or inner classes. A member can be either a class member (static) or instance member (not static). I just told clo1 to try a class member.

And as for being careful with class members: Shyam, you aren't right. Instance methods can access class members. Their values is just the same for all instances. What you mean is the other way round: Class methods can't access
instance members.
Avatar of clo1

ASKER

Yes, it's work. But can you tell me more about how it works? Why I should declare like this? Thanks !
Okay, let's try.

In an object oriented programming language you have two things:

1. class
2. instance

If you say "object" you could mean both things and confuse people. So it is better either to say "class" or "instance" and experts know what you are talking about.

What is a class? One could say, an object.

What is an instance? One could say, a value of the object.

An example:

A dog is a class. Your neighbor has a dog called Lumpi. Lumpi is an instance.

*** ***

Now to the member fields.

An instance member field (without static) is a field which has a different value for every instance. Let's say the age. Lumpi is 6 months younger than the dog of your best friend.

A class member field (with static) is a field which has the same value for all instance. Let's say the number of dogs in your home city. This number is the same for Lumpi and for the dog of your best friend. You could call it a "global" field.

*** ***

And for your problem, I think (I am not sure, because I don't know your program), you are using several instances of the class StockDB. But the member field showSPRC is the same for all instances. It is "global". Exactly the same as the number of dogs in your home city.

Hope, it's clear now. And if not, experiment a little bit with static member fields and static methods.
Avatar of clo1

ASKER

Oic. Thanks a lot. It's clear enough