*
The moose likes JSP and the fly likes rookie in need of jsp help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "rookie in need of jsp help" Watch "rookie in need of jsp help" New topic
Author

rookie in need of jsp help

john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
Im trying to build my first jsp/servlet site and Ive come accross the following problem. When I pass a parameter to a jsp page the variable is shared accross all instances of the page. How do I keep it so that only this page is aware of the parameter? Heres part of my code. Thank you for your time
<%@ page contentType="text/html; session="true"%>
<%!
String choice = null;
String canvasChoice = null;
%>
<html>
<body>
<%
choice = (String) request.getParameter("CHOICE");
canvasChoice = (String) request.getParameter("pager.offset");
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You have declared your variables with a syntax that makes them instance variables. Since there is only one instance of a JSP servlet, they are naturally shared by all requests.
You should look for a JSP book or tutorial to help you get oriented to programming in the servlet environment.
Bill
john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
So if I was to include my variables in the following it should correct the problem right? I have the code at home and I wont be able to test it until tonight.
<%
String choice = null;
String canvasChoice = null;
%>
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
If you're not going to share the variables throughout the application, you might want to look into hidden form fields. This limits the scope of the variables to the page on which they are declared, and the page to which they are submitted.


for (int i = today; i < endOfTime; i++) { code(); }
john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
one more question if I would set
session.setAttribute("objectName", object);
Would every client obtain the same object? Or would I have to make "objectName" a unique name which would identify the user?
Thanks again for all your time.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Winston, there's a world of difference between JSP scripting variables and submission paramaters (aka 'form variables'). I'm not sure what your post was trying to add to the discussion.
john, yes, by removing the ! from the declaration, your scripting variables will become local variables rather than instance variables.
A very useful exercise would be to take a look at the Java servlet code that is generated by the container on the behalf of your JSP. Look at the difference in where code that uses the ! and where code that doesn't use the ! gets placed. You will find it very enlightening.
bear
[ October 28, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

john, it'd be best to ask unrelated question in a new topic.
The purpose of the session is to give every user their own sandbox to play in. So attributes placed onto the session will be unique to a particular 'user' (technically, unique to the set of browser windows that shares the session cookie).
There's lots of info on session management throughout this forum. Take a peek via the search function.
bear
john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
Ill remember that for next time. Thanks again for your time. Youve been quite helpful
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

No problem! That's why we're here!
bear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: rookie in need of jsp help