This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes reseting jsp variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "reseting jsp variables" Watch "reseting jsp variables" New topic
Author

reseting jsp variables

Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
if an error occurs in my jsp page i send the to a different jsp page informing them of the error and a link back to the same page.

but the jsp doesnt respond to the new information, if they type in the same error.

it only works if i restart the server. any ideas what is wrong?


Whats in a name?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

"bobby joe",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
[ March 21, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
basically..

i declare and assign an integer at the top of pageA (private int okay = 0

then depending on other stuff, if the information entered is right i assign it to 1
(okay = 1 and redirect them to pageB

if any thing goes wrong, okay is still 0 so i redirect them to the error page pageC

that all works but you see when they enter the right information (okay = 1) they are redirected to a pageB, but i need them to go back to the previous page pageA(dnt ask why, i just do).

but when i do that and enter wrong data it just goes to pageB and not the error page pageC

i think its because when they go back to pageA from pageB okay is 1 and not 0.
i thought when you go back it just restarts the jsp page from scratch (where private int okay = 0; at the top). it only works when i restart the server (Tomcat).
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
the names robert johnson, im known as bobby but people call me bobby joe instead of bobby johnson, just coz its like my name. dont threaten me about banning me. if ure gona do, do it. id prefer u if u didnt but i really dont see any reason why ure so worked up over a name.

names are meaningless.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Bobby, we don't like closing people's accounts -- we're here to teach and promote Java. But part of maintaining a professional atmosphere (but not too stuffy -- hence the moose) is to use real names.

This naming convention is taken very seriously and it's one of the only firm rules (other than "Be nice") that we have.

So please change your display name to Bobby Johnson or Robert Johnson and all will be well.

thanks
bear
Forum bartender
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
happy now?

can u delete all these stupid posts about names (including this one) as i dont want people getting distracted when they read my initial question.

Thanx.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Thank you.

In your JSP you seem to have fallen into a common trap, and that is to create scripting variables as instance variables.

Remember that your JSP is converted into a servlet by the container. Since only one servlet is ever created and used for the JSP, the instance variables are creating a threading nightmare and do not get reset each time the page is hit.

My first suggestion is to remove all Java from your JSPs completely and use the JSTL, EL and custom actions for the dynamic portions of your pages.

If that's too big a step, at least remove the variables from scriptlet declarations (the <%! %> constructs). This is what is causing your variables to be declared as instance variables.
[ March 21, 2005: Message edited by: Bear Bibeault ]
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
well pageA (the main page) is a servlet, pageB and pageC are jsp pages.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Right, but do you have instance variables in either the servlets or in the JSP pages? They're just bad news all around.
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
what do you mean instance variables?

nearly all of them are declared as private at the top of the servlet

eg
private String varName = null;
private int num = 0;

then i assign them in the doGet method eg

varName = "okay";
num = 1;
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Those are instance variables.

If you are going to be working in the Servlets and JSP environment you need to learn a bit about multi-threading and why instance variables will not work in such an environment.

Have you read through any Servlets books or tutorials?
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
yea, but you see im about 60% into my project already and its due in three weeks, it just isnt possible to change them all. i have about 30 jsp pages and 15 servlets.

if i were to do it again i would use some other session tracking rather than URL rewriting and forms and use Beans but its too late for all that now.

are u saying that there is nothing i can do.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

At minimum you must eliminate instance variables. There's no way around that. They make your servlets/JSPs unusable in the threaded environment of a web app server.

You'll need to factor out the instance variables and decide where the best place to stick the data is.

Some quick rules of thumb that may or may not be helpful:

1) If the data is needed only within the servlet handlers, use automatic variables within the handlers.

2) Data needed for the duration of the request can be stored as request attributes.

3) Per-user data that needs to persist across requests can be stored in the session.

4) Site-wide data can go in the application context.

For 3 and 4, you still need to worry about synchronizing access since multiple threads can access these contexts simultaneously.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Robert Johnson:
yea, but you see im about 60% into my project already and its due in three weeks, it just isnt possible to change them all. i have about 30 jsp pages and 15 servlets.


Then you are about 60% and three weeks from releasing an app with a severe threading problem. It's time to stop everything and get a real good grip on the Servlet/JSP lifecycle and how threading works in a Servlet environment.

Much easier at 60% then at 90% or after the product has been released.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
its not going on the market or anything,... its just a project for college.


id rather have something to show for 7 months work than nothing.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
is there any reason why you cannot declare "int okay=0" inside doGet() instead of making it a class data member? Since you thought it would be wiped out every time the jsp page or servlet reloaded, then doGet (or doPost, or service) are the perfect places for it. If you need it in functions called by doGet, pass it in as parameters or store it in the request (request.setAttribute("okay", new Integer(okay) .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reseting jsp variables
 
Similar Threads
Handling response.redirect errors in JSP page
html error
error message
Java Script error
pagecontext release problem