Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please check my code..

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam using Java Web Server. Iam passing parameter from servlet container to servlet.
here is my code.

[ October 07, 2002: Message edited by: Marilyn de Queiroz ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I give this a try. First of all notice that you have no main function in your servlet. ALL java programs start with a class with a main function!!! That means that somewhere before your class, another class was loaded, probably something like servletrunner.class. This class takes care of everthing needed for persistance etc. It also takes care of creating your servlet instance object.... after it ensures that there is an instance that has ran the init() method. Everytime you make a request you get a new instance of your servlet. Including the first request, one is made for persistance and one is made to execute your doPost, doGet or service method. All this means that since you declared name as an instance variable, you got a new and different copy than the one created when the init method was run. AND it's default value is .... null
You can correct this by making name static. However a work of caution is needed where static variables are concerned with servlets.... they should only be used if they declared final!!!
Hope this clears up things
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right off hand I don't see why it doesn't work. I normally make variables set by the init method be static variables, you might try that.
The comment "Well, I give this a try. First of all notice that you have no main function in your servlet. ALL java programs start with a class with a main function!!!" is totally off the wall. The main convention only applies to stand alone applications as loaded by the java.exe interpreter. Servlets and applets run in a container (engine) that follows different conventions.
WBB
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right that servets and applets are run in containers that are a class that is started with a MAIN method!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic