[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

My Project Deadline - Servlet

Hi,
 I am in the process of completing my first major
 web-based project, or at least am due for my project
 deadline of early next week.
 (which I must complete, or I will be dead)
 
 There are a few mistakes I have made, which I am sure
 must be corrected to avoid major problems.
 Below I have given an example similar to my program
 structure. Now My issues.
 
 myservlet is the servlet accessed by the web page.
 It performs many functionalities, then prints the html output.
 The way this is suppose to work is,
 - user accesses servlet through web-page (with parameters passed)
 - servlet creates an instance of mysubmod which loads information
 from database and creates an object: Note this submod object is
 suppose to be active throughout the entire session, and available
 only to this user.
 - output is printed based on mysubmod
 - x,y (global) are used to mysubmod retain values available only for
 this session.
 
 //everything seems to work ok when I access as a single user. I am
 // under the impression, however that when this interface is used
 //by multiple users, there will be issues with shared global variables
 // So my biggest problem is how to rework this system to handle
 //session variables. Any comment to help me rework this would be
 //greatly appreciated.
 //Many long hours ahead of me...........
 
 
 
 class myservlet extends httpservlet{
 
       int x
       int y
       
       doget(){
       
       //recieves parameters and stores
       //values in x,y
       
       printgui(x,y);
       
       }
 
       public void printgui(int varone, int vartwo){
       
             someOtherPackage.submod mysubmod = new someOtherPackage.submod(x);
             mysubmod.printHtml;
       
       }
 }
 
 
 
 package someOtherPackage;
 
 class submod{
 int A
 int B
 //many global variables
 
 public void submod(int submodrev){
       A = submodrev
       B = dosomething(A);
 
 }
 
 private void dosomething(int myvar){
       do some stuff to myvar;
       return myvar;
 }
 
 public String printHtml(){
       //print stuff + A + B
 }
 

 
 
 }
0
RockyFullen
Asked:
RockyFullen
  • 9
  • 7
  • 7
  • +1
3 Solutions
 
CEHJCommented:
You need to save the object containing the data in the session
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> int x
>> int y

Remove the data-members from the servlet. The same servlet will be used by multiple users. Instead, put them inside the methods as local variables, get their values from the DB and display them. If you want to store them for further reference, save them in the session:

HttpSession session = request.getSession () ;
session.setAttribute ( "X", new Integer ( x ) ) ;
session.setAttribute ( "Y", new Integer ( y ) ) ;
0
 
JavatmCommented:
> If you want to store them for further reference, save them in the session:

Just incase you dont know how to save it by session here is how it works :
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Session-Tracking.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JavatmCommented:
0
 
RockyFullenAuthor Commented:
What about the variables A, B which must be global to the
submod object?

These values are controlled by the servlet,
but must be maintained for the entire session. They are to
be exclusive to the session however.

How do I make these variables
retain their values throughout the session, and remain exclusive to
the session?
0
 
CEHJCommented:
>>How do I make these variables
retain their values throughout the session, and remain exclusive to
the session?

By saving them in the session ;-)
0
 
CEHJCommented:
If they are only assigned once, you can make them the final instance variables of a bean that is then saved in the session
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> How do I make these variables retain their values throughout the session, and remain exclusive to the session?

That is exactly what we explained. Let us know if something is not clear.
0
 
JavatmCommented:
> How do I make these variables retain their values throughout the session, and remain exclusive tothe session?
Cookies, URL Rewriting or Hidden form fields. See my link :D
0
 
JavatmCommented:
Ow well we just all agree on it session :D
0
 
RockyFullenAuthor Commented:
I understand your point on making variable x,y as session
HttpSession session = request.getSession () ;
session.setAttribute ( "X", new Integer ( x ) ) ;
session.setAttribute ( "Y", new Integer ( y ) ) ;

But variables A,B are in a completely different package, and created here
someOtherPackage.submod mysubmod = new someOtherPackage.submod(x);

I do not understand how to get to the variables A,B to assign them as a session variable.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> But variables A,B are in a completely different package, and created here

>> someOtherPackage.submod mysubmod = new someOtherPackage.submod(x);

Where are A and B in your above example? You mean mysubmod?

You can import the packages which contain these classes, create the objects and save them in the session.
0
 
CEHJCommented:
>>But variables A,B are in a completely different package

That doesn't matter. The class 'SubMod' must be given get/set methods and imported
0
 
Mayank SAssociate Director - Product EngineeringCommented:
public class submod
{
  private int x ;
  private int y ;
  public void setX ( int x )
  {
    this.x = x ;
  }
  public int getX ()
  {
    return x ;
  }
  // same way, for Y
}
0
 
JavatmCommented:
> That doesn't matter. The class 'SubMod' must be given get/set methods and imported
If Submod has method parameter has A & B then you can use it by passing a parameter to it.
0
 
CEHJCommented:
If you don't need to mutate it, remove the set method
0
 
Mayank SAssociate Director - Product EngineeringCommented:
And pass x, y to the constructor.
0
 
RockyFullenAuthor Commented:
Ok, so refering to the variables A,B in the class submod,
instance mysubmod..

If I am understanding correctly
These variables must be maintained as session variables
outside of thes mysubmod instance. The variables are
then only to be changed by the get/set methods.

my point being that these variables are not stored in the
object mysubmod, but rather in the session.
Is this correct?

I am new to bean methods as well.
0
 
RockyFullenAuthor Commented:
Also, If I import it...
what does this do for me?
I was not importing....
0
 
CEHJCommented:
>>And pass x, y to the constructor.

It would probably be better to maintain compatibility with bean standards and use an empty ctor. You never know what you may need to do later
0
 
CEHJCommented:
>>
my point being that these variables are not stored in the
object mysubmod, but rather in the session.
Is this correct?
>>

Not quite. They are stored in both
0
 
JavatmCommented:
> Also, If I import it...
> what does this do for me?
> I was not importing....

importing is standard but it depends on you you are actually directly calling it by:
someOtherPackage.submod mysubmod = new someOtherPackage.submod(x);

If you import the package then you can just instantiate it like :
submod mysubmod = new submod(x);
0
 
CEHJCommented:
>>
Also, If I import it...
what does this do for me?
>>

It enables you to access it, create instances, etc. Otherwise you can do nothing without always using the fully-qualified name
0
 
JavatmCommented:
and by calling the package gives you access on all the classes that are available.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> It would probably be better to maintain compatibility with bean standards and use an empty ctor

If you do need to use it as a bean - yes. But then you need the setters to set the properties in the first place :) we were talking about: >> If you don't need to mutate it, remove the set method
0
 
CEHJCommented:
Yes - didn't mean to contradict myself ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
He he ;-)
0
 
RockyFullenAuthor Commented:
I will try your suggestions and get back to you before Mon.
As usual, thanks for the input
I would be lost without forums.....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now