Solved

Global Variable

Posted on 2001-06-13
10
258 Views
Last Modified: 2010-05-18
   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>");
                    }
                }
            }
0
Comment
Question by:clo1
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:exorcist
Comment Utility
may I know how you managed to declare a GLOBAL variable?
0
 
LVL 2

Author Comment

by:clo1
Comment Utility
I have post it on the above sample:

private boolean showSPRC;
0
 
LVL 3

Accepted Solution

by:
dnoelpp earned 200 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:exorcist
Comment Utility
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
 
LVL 3

Expert Comment

by:superschlonz
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:shyamkumarreddy
Comment Utility
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
 
LVL 3

Expert Comment

by:dnoelpp
Comment Utility
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
 
LVL 2

Author Comment

by:clo1
Comment Utility
Yes, it's work. But can you tell me more about how it works? Why I should declare like this? Thanks !
0
 
LVL 3

Expert Comment

by:dnoelpp
Comment Utility
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
 
LVL 2

Author Comment

by:clo1
Comment Utility
Oic. Thanks a lot. It's clear enough
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now