Scope of instance variables in servlets..

Hi all,
          Sorry for asking a dumb question..
          What is the scope of instance variables in a servlet? Is the scope is
          limited to thread?
          In other words, in the following example, i am setting "testStr", in one
          request and when i tried to access the same instance variable from another
          request, i am getting it as null. Does it mean instance variable is limited
          to that particular thread/request?
          thanks in advance..
          -ramu
          

Oops ... I had misunderstood and had the problem backwards ;-)
          > Is it known behavior? With registered servlet its working fine (a
          > instance variable is shared by all requests)
          I believe so; I typically deploy in a WAR so have not seen the problem that
          you describe. Servlets can be reloaded, so what you saw could have been
          caused bye a date/time mismatch between the .class file and the server's
          current time. On the other hand, that could be how WebLogic works.
          Peace,
          Cameron Purdy
          Tangosol, Inc.
          http://www.tangosol.com
          +1.617.623.5782
          WebLogic Consulting Available
          "Ramu" <[email protected]> wrote in message
          news:[email protected]...
          > Hi Purdy,
          >
          > I got it. I am testing the servlet as a unregistered servlet, which is
          > creating instance for every new request!!!, which created this whole
          > confusion. Is it known behavior? With registered servlet its working fine
          (a
          > instance variable is shared by all requests)
          >
          > > What theory? ;-) Instance variables are on the object, and the object
          can
          > > be used by multiple threads, thus allowing multiple threads to access
          the
          > > same instance variables.
          > what i mean by theory here is, all instance variables in servlet should
          be
          > shared by all requests. Now i got it sir.
          >
          > Thank you..
          > -ramu
          >
          >
          > >
          > > Peace,
          > >
          > > --
          > > Cameron Purdy
          > > Tangosol, Inc.
          > > http://www.tangosol.com
          > > +1.617.623.5782
          > > WebLogic Consulting Available
          > >
          > >
          > > "Ramu" <[email protected]> wrote in message
          > > news:[email protected]...
          > > > Hi,
          > > >
          > > > > No, an instance variable in a servlet class is not limited to a
          > thread.
          > > > There
          > > > > is exactly one copy of the servlet created in memory and all
          requests
          > > pass
          > > > > through the same instance, thus sharing any instance variables. A
          > > couple
          > > > of
          > > > > things to remember:
          > > > I totally agree with you, But i am wondering why my sample servlet(i
          am
          > > > attaching the file) is not sharing the instance variables across
          > > > threads/reqs. (in 1st request set some string to "testStr" instance
          > > > variable, in next request if you read the same instance variable, you
          > will
          > > > get null!!)
          > > > Our current code is having instance variables. But right now we are
          not
          > > > getting any problems. But as per theory, instance variables should be
          > > shared
          > > > across threads/requests..
          > > >
          > > > Any how we are changing our code to remove all instance variables.
          > > >
          > > > thanks,
          > > > -ramu
          > > >
          > > > >
          > > > > 1.) Using instance or class variables in servlets that are not
          > read-only
          > > > is not
          > > > > a good thing to do. This makes the servlet not thread-safe. To
          > > maintain
          > > > > variables across requests, sessions, etc., use the servlet-defined
          > > objects
          > > > to
          > > > > store the state (e.g., request, session, etc.).
          > > > >
          > > > > 2.) If you modify the servlet class on disk, WebLogic will reload
          the
          > > > servlet
          > > > > class thus discarding the old instance and creating a new one (of
          > > course,
          > > > this
          > > > > depends on some configuration parameters for servlet reloading).
          > > > >
          > > > > Hope this helps,
          > > > > Robert
          > > > >
          > > > > Ramu wrote:
          > > > >
          > > > > > Hi,
          > > > > > thanks for quick reply.
          > > > > > I am not at all using SingleThreadModel. See the following code.
          > > > > > In first request i am passing "abc" value to testStr. But in
          second
          > > > request,
          > > > > > I am getting null for testStr in second request. It looks like
          (for
          > > me)
          > > > for
          > > > > > non single threaded model also, scope of instance variables is
          > > limited
          > > > to
          > > > > > Thread/Request.. But as per theory, because its not single
          threaded
          > > > model,
          > > > > > only one instance should be there AND testStr(instace variables)
          > > should
          > > > be
          > > > > > shared across the all threads. But i am not able see that
          behaviour
          > > > here.
          > > > > > But if declare instance variable as static, o am able to share
          it
          > > > across
          > > > > > all threads/requests.
          > > > > >
          > > > > > note:
          > > > > > From browser i am setting testStr to "abc" with URL like
          > > > > > "localhost/test1?testStr=abc"
          > > > > > And 2nd req from browser is like "localhost/test1" (on server
          > output
          > > i
          > > > am
          > > > > > getting null for testStr)
          > > > > >
          > > > > > Any ideas?
          > > > > >
          > > > > > thanks,
          > > > > > -ravi
          > > > > >
          > > > > > import java.io.*;
          > > > > > import javax.servlet.*;
          > > > > > import javax.servlet.http.*;
          > > > > >
          > > > > > public class test1 extends HttpServlet
          > > > > > {
          > > > > > public String testStr;
          > > > > >
          > > > > > public void doGet (HttpServletRequest req, HttpServletResponse
          res)
          > > > > > throws ServletException, IOException
          > > > > > {
          > > > > > doPost(req, res);
          > > > > > }
          > > > > >
          > > > > > public void doPost (HttpServletRequest req,
          HttpServletResponse
          > > res)
          > > > > > throws ServletException, IOException
          > > > > > {
          > > > > > try {
          > > > > > res.setContentType("text/html");
          > > > > > PrintWriter out = res.getWriter();
          > > > > > if(req.getParameter("testStr") != null)
          > > > > > {
          > > > > > testStr = req.getParameter("testStr");
          > > > > > System.out.println("set testStr = " + testStr);
          > > > > > }
          > > > > > else{
          > > > > > System.out.println("get testStr = " + testStr);
          > > > > > }
          > > > > >
          > > > > > }
          > > > > > catch(Exception e){
          > > > > > e.printStackTrace();
          > > > > > }
          > > > > >
          > > > > > }
          > > > > > }
          > > > > >
          > > > > > "Cameron Purdy" <[email protected]> wrote in message
          > > > > > news:[email protected]...
          > > > > > > Yes or no, depending on if your servlet implements
          > > SingleThreadModel.
          > > > > > >
          > > > > > > See the servlet 2.2 spec for documentation on how many instances
          > of
          > > a
          > > > > > > servlet will be created. See the doc for the SingleThreadModel
          > > > interface.
          > > > > > >
          > > > > > > Peace,
          > > > > > >
          > > > > > > --
          > > > > > > Cameron Purdy
          > > > > > > Tangosol, Inc.
          > > > > > > http://www.tangosol.com
          > > > > > > +1.617.623.5782
          > > > > > > WebLogic Consulting Available
          > > > > > >
          > > > > > >
          > > > > > > "Ramu" <[email protected]> wrote in message
          > > > > > > news:[email protected]...
          > > > > > > > Hi all,
          > > > > > > >
          > > > > > > > Sorry for asking a dumb question..
          > > > > > > >
          > > > > > > > What is the scope of instance variables in a servlet? Is the
          > scope
          > > > is
          > > > > > > > limited to thread?
          > > > > > > >
          > > > > > > > In other words, in the following example, i am setting
          > "testStr",
          > > in
          > > > one
          > > > > > > > request and when i tried to access the same instance variable
          > from
          > > > > > another
          > > > > > > > request, i am getting it as null. Does it mean instance
          variable
          > > is
          > > > > > > limited
          > > > > > > > to that particular thread/request?
          > > > > > > >
          > > > > > > > thanks in advance..
          > > > > > > >
          > > > > > > > -ramu
          > > > > > > >
          > > > > > > >
          > > > > > > >
          > > > > > >
          > > > > > >
          > > > >
          > > >
          > > >
          > > >
          > >
          > >
          >
          >
          

Similar Messages

  • Doubt about scope of instance variable

    Hi All,
    i have a doubt about scope of instance variable.
    if i declare a instance variable in servlet , i want to know whether it can be shared(means everywhere ) or not.
    thanks in advance
    Gopal

    The servlet container will create one servlet object, and run multiple threads through its service() / doGet() / doPost() methods.
    That means that any instance variables of the servlet are shared between multiple requests, and can cause problems with threads accessing the same variable..
    To make your servlets thread-safe
    1 - have no instance variables - only use local variables in the doGet/doPost method.
    2 - use the session scope for storing variables you need over multiple calls.
    Cheers,
    evnafets

  • Instance variables in Servlets.

    Hi
    Is it a good practice to use instance variable in servlet.
    Please refer code snippet below.
    public class ExcelAction extends HttpServlet {
    private int var1 =0;
    doGet () {
    var1++;
    fun1();
    fun1 (){
    var1++;
    }Like in above code I have used an instance variable var1 because I wish the same to be accessible in all servlet functions.
    I guess it is not a good practice, as different requests to this servlet at same time will result in multiple threads of this servlet. Which will make the state of var1 unsafe.
    If this is correct then what are the alternates if I wish to share variables between functions in a servlet.
    Thanks

    money321 wrote:
    Because in beginning I was not sure that Servlets instances are also actually threads.They are not not threads. Each call to doGet(), doPost() etc is called in a Thread and since there can be many simultaneous calls each having it's own thread a Servlet has to be thread safe.
    My other question was specific to threads as I was trying to implement threads.Why?
    >
    But in this one I rather used servlets.
    But apologies, as instance of servlet are also threads.As I said, they are not threads.
    >
    But still one last doubt.
    Sabre if you could please clarify the same as well :How about you do some reading [http://java.sun.com/developer/onlineTraining/Servlets/Fundamentals/|http://java.sun.com/developer/onlineTraining/Servlets/Fundamentals/] and use Google.
    >
    in your option 2 and three I will have to pass request object to function fun().No. You will need access to the request object for 2 and the servlet context for 3. You need to pass something
    If i am correct on this then is this too a better approach ?My preferred approach is 1 since it makes much of your code testable outside of a Servlet.

  • Scope of instance variables clarification requested

    under the topic "Scope of instance variables" in the class notes for the course i'm doing it is stated:
    inside the class definition, the name can be prefixed by the keyword "this".
    e.g. available = this.balance + overdraft;
    in simple words, please explain what it means
    or:
    what is a class definition? and what does using "this" mean?
    i'll gladly take internet links on where i can find the answer to my own question if you don't have time.
    thanks a million!

    this can be regarded simply as a reference variable which points to the intance of the class it's used in.
    So this.fieldOne referer to the instance field fieldOne withing the current instance just as theOther.fieldOne refers to the instance field in another object.

  • Access of Instance variable in servlet

    hi,
    If i declare a instance variable in servlet , can i access that variable throughout my application??.
    please let me know.
    thanks in advance.

    Servlets are Java classes. They follow all the rules of Java.

  • Stateless Bean - scope of instance variable in EJB Timer call back function

    Hi,
    I would like to know on the scope of an instance variable of a Stateless Bean object,
    when used in a EJB Timer call back.Let me explain this in more detail below.
    I have a requirement to use a EJB Timer.
    For this, I have created a stateless object since Timer creation needs to be done
    from a stateless bean. I have a member variable "count" of the stateless bean class.
    In the timer call back(ejbTimeout), I am able to use this count variable during
    each time of the call back, and the value of this variable is also updated properly.
    I have a few queries with respect to the above behaviour:
    1) Does stateless bean object not get destroyed once the Timer is created from the Bean?
    2) If the Bean object is not destroyed, then when does the bean object get destroyed?
    3) If both (1) and (2) are not true, then can anyone explain on how the above behaviour is possible?
    Thanks in advance,
    Ulrich

    Hi Ulrich,
    The ejb timer is associated with the stateless session bean component, not with a particular bean instance. There is no formal relationship between the bean instance that called createTimer() and the bean instance on which the timer callback happens. If they're the same in your test run that's just a coincidence and not something your application should be depending on.
    In the stateless session bean model, the container can create and destroy stateless session bean instances at any time. The container is free to pick any stateless session bean instance to service any client invocation or timer callback. If you need to pass context into a timer callback, one way to do it is via the timer "info" object. However, the info object is immutable so it wouldn't be a good match for a counter. You could of course always just use a database for any necessary coordinated state.
    --ken                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Servlet Instance Variable scope

    Just want to clarify my understanding of instance variables in regards to Servlets.
    public class TestServlet extends HttpServlet
        public String instVariable = "";
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
          instVariable = request.getParameter("param");
          response.sendRedirect( instVariable ) ;
    }If two users both enter the processRequest logic at the same time, is it possible that they will both get the same
    result? My understanding of this was that if they each execute the first line, then each execute the second
    line, each user would be redirected to the same instVariable that was set by the second request.
    What would be the easiest way to stop this from happening? syncronized? ThreadSafe?
    I just want a simple clarification.
    Thanks for the insight.

    If two users both enter the processRequest logic at
    the same time, is it possible that they will both get
    the same
    result? My understanding of this was that if they
    each execute the first line, then each execute the
    second
    line, each user would be redirected to the same
    instVariable that was set by the second request. Yes, it is possible, since there is only one instVariable.
    What would be the easiest way to stop this from
    happening? syncronized? ThreadSafe? The easiest way to avoid that is to simply not use an instance variable. Use a local variable, declared within your processRequest method. Each thread (i.e. each user of the servlet) has its own set of local variables.

  • Servlet instance variables

    This might be a dumb question, but I'll ask it anyway...
    I have methods in my servlet to handle registration, specifically creating address, customer and order objects.
    All the information needed for creating my objects is coming from the registration form.
    Right now if I were to call the createAddress() method, I have to pass the request object.... createAddress(request);
    Would it be "thread safe" to make an instance variable of the request and refer to it using getters/setters? Or would it be wiser (and best practice) to make instance variables for the request attributes (like firstName, lastName, etc.)? Or am I way off and shouldn't be doing any of the above?
    Thanks for the help.

    GB_java_guy wrote:
    To answer you question, I think it's different, from what I've read, because request attributes are supposed to be thread safe and I'd be using the request attributes instead of the request object.You said something about "making instance variables for the request attributes." I don't know what you mean by that, but if it's instance variable in the servlet class, don't do it.
    I have no clue what you mean by "request attirbutes intead of request object."
    So are you saying I should just use the request object and pass that to my methods instead? In general, yes. In some cases you may want to pass individual pieces of it--e.g. if there's a method that doesn't need to know or care about the fact that you're operating in a servlet context, and just needs a couple values to do its job.

  • Servlets and their instance variables

    I understand that for every request to a servlet, a new thread handles those requests. So it's one request, one thread. But how about instance variables of a servlet class? Are they also one instance variable per thread/request or just like the servlet, one instance?

    hi, its exactly as you expect - one instance at all. all threads are working with one intance. you can indeed finetune the number of instances of a servlet using the
    <load-on-startup/>tag in the web.xml file, but its up to container if multiple thread will be using many instances.
    you can also implement the (deprecated) SingleThreadedModel interface which flags the container not to share that servlet instance to other threads. But this shall not be used in productive environments.
    so its always a good idea to put your business impleementation to another class and only use the servlets to connect your business implementation to http like
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{
       new MyBusinessLogic().perform(request.getInputStream(), response.getOutputStream());
    }

  • How can I use evaluate to get the instance variable in customized tag

    1.
    At first , I create a class called bean,and declared several params in it and do not define any getter function for the param.
    class bean{
    String param = "test";
    SomeClass scObj = new SomeClass();
    2.
    The second ,I use
    request.setAttribute("beanObj",new bean());
    3.
    And then I wanna use the customized tag to show a text box , then initialize it's value.
    <salt:text name="param" value="beanObj.param">
    <salt:text name="obj" value="beanObj.scObj.func()">
    4.
    I tried the evaluator provided by JexlContext ,Struts, JSTL and it seems that if I do not define the getter for the variable ,I can not get the bean's instance variable's value.
    Expression e = ExpressionFactory.createExpression( value );
    JexlContext jc = JexlHelper.createContext();
    jc.getVars().put(strInitBeanName, request.getAttribute("beanObj"));
    Object obj = e.evaluate(jc);
    the result of the obj is null....
    Can anybody recommand some other evaluator can get the value of a instance variable from an object?

    do you have any other suggestion ? Nops, somebody else may have though. AFAIK, all lookups of the type
    beanName.propertyNameuse reflection on the getXXX() methods to access the property.
    Having said that, I guess you could write one though in a custom tag, using the same - reflection (you will ahve to rely on the java.lang.reflect.Field class quite heavily) - but that would be reinventing the wheel for most other functionality that you would have to include (like looking up the bean in scope etc)
    cheers,
    ram.

  • Binding: Instance variable loses value.

    Hi all,
    Just making my first steps into Objective-C, I've done a lot of C, C++ and C# on win & linux. Anyhow, I've been hacking away happily and discovered a peculiar behaviour, and I'm not sure if it's my code or some obscure bug in Xcode.
    1. I have a Check Box(NSButton) directly bound to a BOOL instance variable called "checkValue" using KVC. The containing class is a custom NSView subclass.
    2. I manually implemented the setter according to KVC naming rules "setCheckValue".
    3. When debugging the UI (check box) calls the setter perfectly with the correct value, which shows up in the NSLog output.
    4. I hooked up the action for the check box as well, and inside the action handler the instance variable reports the value correctly. Everything looks fine.
    5. Now here's the rub. I put a mousedown event handler into the class as well, and it is called flawlessly when I click on the custom view. However, "checkValue" does not report the correctly set value.
    So, how can a class instance variable, which is set and reports correctly elsewhere in the same scope, all of the sudden take on a nonsense value in a event handler? If a variable has a value set, it should be the same everywhere within the same scope!
    Below is simplified code extracted from the original project for clarity. It produces exactly the same behaviour as the more complex project without the distracting code.
    #import "CheckBoxHandler.h"
    @implementation CheckBoxHandler
    @synthesize checkValue;
    //Apparently this is required for binding in NSView based classes
    //The same behaviour occurs even if you remove this.
    +(void)initialize
    [self exposeBinding:@"checkValue"];
    -(id)initWithFrame:(NSRect)frameRect
    self = [super initWithFrame:frameRect];
    if(!self)
    return nil;
    [self setCheckValue:YES];
    return self;
    //manual impelementation of KVC setter
    -(void)setCheckValue:(BOOL)v
    checkValue = v;
    NSLog(@"Setter Called: checkValue is %d", checkValue);
    //The action works fine as evidenced by Log output
    //Check and uncheck the box a few times.
    -(IBAction)checkBoxAction:(id)sender
    NSLog(@"UI State:%d Ivar State: %d", [checkBox state], checkValue);
    //Mouse clicking on the view calls into this event.
    //The problem is the value reported by [self checkValue] OR checkValue directly
    //do not match the UI state. In fact it always reports some nonsense value.
    -(void)mouseDown:(NSEvent *)event
    NSLog(@"Inside Mousedown: checkValue is: %d", [self checkValue]);
    @end
    I have a workaround, but it really bends my head when something doesn't behave as expected!

    I have synthesized against "checkValue", although I manually implemented the setter so I could set a breakpoint and observe the value. I thought [self checkValue] and self.checkValue both simply call the same getter, just different syntax.
    The setting of the value using the UI checkbox happens outside of the event call chain. I can check and uncheck the checkbox and the value changes correctly. The checkbox is in a separate space on the window, so clicking it does not activate mousedown on the view, this is correct behaviour. Clicking on the view does activate the mousedown event, this is done after I've set the checkbox so the value has been set well before the mousedown event. Thus it should already have the correct value in the event.
    For completeness this is the interface file:
    #import <Cocoa/Cocoa.h>
    @interface CheckBoxHandler : NSView {
    IBOutlet NSButton *checkBox;
    BOOL checkValue;
    -(IBAction)checkBoxAction:(id)sender;
    @property (assign, readwrite) BOOL checkValue;
    @end
    I'll change everything to self.checkValue just for the excercise. Ok, done. checkValue still reports a value of 1 in the mousedown event regardless of the value set elsewhere.

  • Problem in recognising instance variables declared in the class

    Hi all,
    I am trying to deploy an Servlet desiged in WebObjects5.1 application server on application on Iplanet Application server 6.5
    Back ground: All of you might be aware that WebObjects5.1 is an J2ee compliant application server. When an application is designed in WebObjects, it creates a WAR file which can be deployed on any J2ee complaint application server.
    I tried to create such application in WebObjects and tried to deploy on IplanetApplication server6.5(running on Windows using IIS web server).
    I am able to successfully deploy the application. But inorder to do this i had to manually add ias-Web.xml file to the war file generated( gave some arbitrary global id). The aplication just consists some string displaying hello world.
    Now when i try to deploy an application which has some instance variables, the instance variables are not getting recognised. This application works fine on WebObjects5.1. But variables are not getting recognised when deployed iplanet application server
    Has any one faced such kind of problem earlier? If yes please help me out
    Thanks
    Venkatesh

    Hi,
    Please send me your servlet code and war file.
    Thanks

  • Instance of each servlet

    I have a web page ,JSP, which calls different servlets for DB stuff. My question is this : will there be a new instance of each servlet for each user when connecting to the the website or should I do something about it.

    hello again
    could you also tell me whats the correct way to do
    this. As far as I know I can mix the Model and
    controller inside a servlet, but with this fixture
    everything will go nuts! do you think I should use
    the servlet as a controller and a javabean as a
    model. OR something else.
    Thanks again for the help.I was in your situation 1 or 2 years ago, and I would just like to share what I've found and try to explain in an easy manner what technologies you could use, and how they work.
    The problem with servlets is that each connection to the same servlet will create a new thread. This is different from the classical CGI approach, where each connection starts a new process.
    The consequence is that you end up coding in a multithreaded environment, with all the hassles with deadlocks, race conditions, problems with shared variables (as mentioned in previous posts) etc.. These problems are difficult to solve, and time consuming. The reason for the multithreaded solution is that it provides a significant performance boost over the "new process" solution. However, it is obvious that a developer doesn't want to worry about multithreading when he/she has a deadline to comply with ;) So, you ask, should I just scrap Servlets or is there a better solution?
    Well, to get rid of this problem for developers, Sun proposed a standard to reduce the complexity of web development. It's called Java EE (http://java.sun.com/javaee/5/docs/tutorial/doc/)
    By maintaining persistent (long lasting) state in a database, and temporary state (session state etc) in a controlled manner, Java EE proposed a way of getting rid of the classical problems in Java web development, and also added several cool new features.
    When using Java EE, you have a special type of beans to save persistent state, they're called entity beans. If you have defined a entity bean (wrote an ordinary java class, and supplied some settings), you can create a new entity bean, set its properties, and tell the Java EE system to save it to your database. You can also search among saved ones (something like "select p from Person as p where p.username='foo' and p.cousin.name='bar'"), let them reference each other and all sorts of neat things.
    There are also beans which are supposed to perform logic using your entity beans, and they're called session beans. They have methods containing your business logic, namely methods like 'getLatestForumPost(), or 'createUser(String name)'...
    The session beans can also save temporary state. If they do so, they're called "stateful session beans". The state they keep is stored like ordinary class attributes, but Java EE makes sure that you won't get thread problems (even though there still are multiple threads). For example, you can use this state to keep track of items in a shopping cart (based on the current user). If several users want their own cart, Java EE just creates a new stateful session bean for each user, you don't have to modify your bean in any way!
    Until now, I've used the word "Java EE", as if it's an actual product, but as I mentioned, it's just a standard. Since it is just a standard, telling how things should work, there are several companies that have made solutions (programs) that actually make things work like this. An application that "make thing work" is called a Java EE Application Server. You need to start the application server in order to run you own Java EE program (you program will be almost like a plugin to the server, or you could say that you program runs within the server). Behind the scenes, the application server usually uses ordinary Servlets to process each request, but it handles all the multithread problems (and others) for you. If you for some reason would like to, you can always gain finer control by using Servlets directly.
    When a user connects to your computer, the server can automaticly identify the user, give him some cookies identifying him, ask for login (if you told the server to do so), and then hand over control to you, telling you this persons login name, what he asked to do, amongst other things, which saves you a lot of hassle. You can alway tell the server how to behave in different situations (for example wich pages or requests that require login etc), by writing some xml config files for each application.
    The application server also lets you send mails from your session beans, send messages between computers, run your application on clusters, parse XML and several of other neat things. It's a dream for a web application developer.
    I hope this will inspire you to have a look at these things, because it really simplifies things, and if you learn it correctly, you can also make some serious money as a Java EE developer ;)
    The only downside is that it seems pretty complex in the beginning, and that it's pretty hard to understand all the vague definitions in the docs etc... I would recommend that you download the Application server from Sun (http://www.sun.com/software/products/appsrvr/index.xml), read the tutorial at http://java.sun.com/javaee/5/docs/tutorial/doc/ and start to write some own programs.
    Don't be passive, just try everything out and you'll figure out that it's less complicated than it seems at the first glance.
    I really hope that this helps!, and sorry for my bad English.
    Best regards
    /Alexander T

  • BufferedReader bf;       is this also treated as instance variable?

    hi i m just now finished my assignment and doing documentation
    and i got a single line here
    BufferedReader bf; //instance variable
    constructor{
    bf = new BufferedReader(new InputStreamReader(System.in));
    }and now i m doing documentation suddenly i got confused whether i should
    write this as instance variable or not. should i?? or shouldn't i
    i think this is obejct,,,,,,,but..i think it is instance variable as well..coz
    it is located in the class scope..confusing.....
    or not..

    hi i m just now finished my assignment and doing
    documentation
    and i got a single line here
    BufferedReader bf; //instance variable
    constructor{
    bf = new BufferedReader(new
    w InputStreamReader(System.in));
    }and now i m doing documentation suddenly i got
    confused whether i should
    write this as instance variable or not. should i?? or
    shouldn't i
    i think this is obejct,,,,,,,but..i think it is
    instance variable as well..coz
    it is located in the class scope..confusing.....
    or not..Okay, first of all constructor {} isn't valid Java syntax, I'm assuming you just used it for brevity. Second, yes bf is an instance variable because it's not a) static (class-) variable or b) local variable.
    If you thought instance variables refer only to primitives (int, byte etc.) you're wrong. Object references can also be instance variables.
    So yes, it is both an object reference and an instance variable.
    Sincerely,
    Jussi

  • Parsing XML and Storing values in instance variable

    hi,
    i'm new to XML.
    here i'm trying to parse an XML and store their element data to the instance variable.
    in my main method i'm tried to print the instance variable. but it shows "" (ie it print nothing ).
    i know the reason, its becas of the the endElement() event generated and it invokes the characters() and assigns "" to the instance variable.
    my main perspective is to store the element data in instance variable.
    thanks in advance.
    praks
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    public class mysax extends DefaultHandler
         String ctelement;
         CharArrayWriter contents;
         String vname1,vrcbreg1,vaddress1,vcountry1,vtelephone1,vfax1;
         String vname,vrcbreg,vaddress,vcountry,vtelephone,vfax;
         public mysax()
              vname1 = null;
              vrcbreg1 = null;
              vaddress1 = null;
              vcountry1 = null;
              vtelephone1 = null;
              vfax1 = null;
              contents= new CharArrayWriter();
         public void doparse(String url) throws Exception
              SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    ParserAdapter pa = new ParserAdapter(sp.getParser());
    pa.setContentHandler(this);
    pa.parse(url);          
         public void startElement(String namespace,String localName,String qName,Attributes atts)
              ctelement = localName;     
         public void endElement(String uri,String localName,String qName) throws SAXException
         public void characters(char[] ch,int start, int length) throws SAXException
              try
                   if(ctelement.equals("name"))
                        vname = new String (ch,start,length);
                        System.out.println("The method "+vname1);
              }catch (Exception e)
                   System.out.println("The exception "+e);
         public static void main(String args[])
              try
              mysax ms = new mysax();
              ms.doparse(args[0]);
              System.out.println("the contents name "+ms.vname1);
              catch(Exception e)
                   System.out.println("this is exception at main" +e);
    my XML looks like
    <coyprofile_result>
    <company>     
    <name>abcTech</name>
    <rcbreg>123456789</rcbreg>
    <address>Singapore</address>
    <country>sg</country>
    <telephone>123456</telephone>
    <fax>123155</fax>
    </company>
    </coyprofile_result>

    I believe that the problem has to do with the value you assign to ctelement. You are assigning the value of localName to ctElement, however for the element: <name>...</name> the localname is empty string i.e. "", but qName equals "name". Because you are assigning empty string to ctElement, when you do the comparison in characters of ctElement to "name" it will always be false. So in startElement change it to ctElement = qName; Try it and see if it works. I have produced similar programs and it works for me.
    Hope this helps.

Maybe you are looking for

  • Will Motion 5 work with the NVIDIA GeForce 8600M GT with 256 MB?

    I have a 15" MacBook Pro Intel Core 2 Duo with a NVIDIA GeForce 8600M GT with 256 MB and 4 GB RAM.  I need to know if this will work with Motion 5?

  • Not enough memory to run YAST

    Zen 6.5 sp2 I can neither PXE boot or boot from CD with any box that has an AMD processor. These also have Biostar motherboards. I get the following message "Your computer does not have enough memory to run YAST", machines have plenty of memory. Work

  • Pricing requirement: condition to be in(active) linked to another condition

    Hi, I have created 2 new conditions type YR05 and YR06 in pricing procedure. I would like that the second one (YR06) is considered as active only if amount of condition type YR05 is different from 0. (KOMV-KEBTR and KOMV-KWERT different from 0) Could

  • Exchange 2013 Services Will Not Start

    Server is a new setup with Server 2012 Standard. Installed Exchange 2013. Got everything up and running correctly. Installed MySQL in prep for another application that required MySQL. MySQL installed C++ 2010 and required CGI for IIS. E-mail immediat

  • How to setup XI (specifically for receiving mail+attachment)

    Hi, I am new to XI. After having had a training for 5 days I have my first assignment and immediately I face a lot of problems (as expected....:-)) Situation is as follows. I have to connect 2 non-sap system via XI. External system 1 is a mailserver