Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Global Variable

Posted on 2001-06-13
10
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

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

Author Comment

by:clo1
ID: 6185367
I have post it on the above sample:

private boolean showSPRC;
0
 
LVL 3

Accepted Solution

by:
dnoelpp earned 800 total points
ID: 6185408
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:exorcist
ID: 6185415
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
ID: 6185424
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6186072
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
ID: 6186153
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
ID: 6189143
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
ID: 6189727
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
ID: 6189744
Oic. Thanks a lot. It's clear enough
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

609 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