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>");
                    }
                }
            }
LVL 2
clo1Asked:
Who is Participating?
 
dnoelppConnect With a Mentor Commented:
Problem 1.

I don't see any problem with your "global" variable showSPRC. To be correct, this is not a global variable, but just a member. To be as exact as possible, it is a private instance member.

Maybe what you want is a class member. Declare it this way:

  static private boolean showSPRC

But then, if you assign it a value, its value will be the same for all instances of your class StockDB.

Problem 2.

Servlet debugging seems to be difficult. Some products say they do servlet debugging, but this never worked for me. I used the plain old System.out.println() and look in the standard output for results. As in old times. So find out where the standard output goes in your web server and use System.out.println(). I find it powerful enough to be happy with it.
0
 
exorcistCommented:
may I know how you managed to declare a GLOBAL variable?
0
 
clo1Author Commented:
I have post it on the above sample:

private boolean showSPRC;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
exorcistCommented:
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.
0
 
superschlonzCommented:
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.
0
 
shyamkumarreddyCommented:
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
0
 
dnoelppCommented:
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.
0
 
clo1Author Commented:
Yes, it's work. But can you tell me more about how it works? Why I should declare like this? Thanks !
0
 
dnoelppCommented:
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.
0
 
clo1Author Commented:
Oic. Thanks a lot. It's clear enough
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.