We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

My Project Deadline - Servlet

RockyFullen
RockyFullen asked
on
Medium Priority
295 Views
Last Modified: 2013-11-24
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
 }
 

 
 
 }
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
You need to save the object containing the data in the session

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
>> 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 ) ) ;
Commented:
> 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

Author

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?
CERTIFIED EXPERT
Top Expert 2016

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

By saving them in the session ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
If they are only assigned once, you can make them the final instance variables of a bean that is then saved in the session
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.

Commented:
> 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

Commented:
Ow well we just all agree on it session :D

Author

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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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
}

Commented:
> 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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
If you don't need to mutate it, remove the set method
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
And pass x, y to the constructor.

Author

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.

Author

Commented:
Also, If I import it...
what does this do for me?
I was not importing....
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
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

Commented:
> 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);
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
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

Commented:
and by calling the package gives you access on all the classes that are available.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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
CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes - didn't mean to contradict myself ;-)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
He he ;-)

Author

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.....
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.