Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

rookie in need of jsp help

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ill remember that for next time. Thanks again for your time. Youve been quite helpful
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem! That's why we're here!
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic