tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mike Curwen" <gb_...@gb-im.com>
Subject RE: Strange request.setAttribute() problem
Date Mon, 28 Jul 2003 17:22:10 GMT
<%!
     private int itemnumber = -1;
%>

Using a ! makes it scoped globally to all requests. If you check the
source file of your JSP you'll see it's declared outside the service
method.
 
That's why different browsers will 'appear' as though they are mixing up
things.  It's because your variable is available on basically, an
application level.

Also, from your initial post...  using getParameter() won't work to
retrieve a value that was set with setAttribute().  You need to use
getAttribute().  That may have been a typo though.



> -----Original Message-----
> From: Charles So [mailto:charles_so@mac.com] 
> Sent: Monday, July 28, 2003 12:11 PM
> To: Tomcat Users List
> Subject: Re: Strange request.setAttribute() problem
> 
> 
> >
> 
> I believe each browser (Opera, Netscape, or whatever) *creates* a  
> unique Request object in Tomcat. I have checked this, and this is  
> indeed true.
> 
> By the look of things, it seems that although Request objects 
> are not  
> the same, the variable stored in them will reference the same value.
> 
> 
> Tim, are you saying if browser A posts a form to TC, the variables  
> within the form will be treated as static variables and 
> visible to the  
> JSP page displaying the variables in browser B, C, D ... ????
> 
> 
> 
> Let me be a bit more specific:
> 
> 
> The   BillingShippingHome.jsp has this code:
> 
> <%!
>      private int itemnumber = -1;
> %>
> <%
> if ( request.getParameter("itemnumber") != null ) {
>      try {
>          itemnumber =  
> Integer.parseInt((String)request.getParameter("itemnumber"));
>      } catch (NumberFormatException ex) {
>          System.out.println("No");
>      }
> }
>          System.out.println("&&itemnumber is " + itemnumber); 
> %> <form name="abc" method="post" 
> action="/mall/WelcomeServlet"> <input type="hidden" 
> name="itemnumber" value=""> ... </form>
> 
> 
> Servlet A:
> 
> 	doPost(HttpServletRequest request, HttpServletResponse 
> response)  
> throws IOException, ServletException {
>         gotoPage("/BillingShippingHome.jsp", request, response);
> 	}
> 
> 
> --------------------------------------------------------------
> ---------- 
> ------------
> Sequence of result :
> 
> 
> 
> Browser A displaying BillingShippingHome.jsp
> 
> itemnumber = -1
> <.. change the itemnumber to say, 0 via javascript... and post this  
> form to Servlet A ...>
> goes back to BillingShippingHome.jsp
> 
> itemnumber now = 0  [correct]
> 
> 
> 
> Browser B now displays BillingShippingHome.jsp
> 
> itemnumber = 0!!!!! [should be -1]
> 
> 
> 
> What gives?
> 
> 
> 
> 
> > -----Original Message-----
> > From: Tim Funk [mailto:funkman@joedog.org]
> > Sent: Monday, July 28, 2003 07:53
> > To: Tomcat Users List
> > Subject: Re: Strange request.setAttribute() problem
> >
> >
> > This is the correct behavior. In this scenario, you are operating on
> > the
> > request, not the session.
> >
> > request.getParameter --> Get stuff from the query string (or input
> > stream if
> >
> > post) - a per request thing
> >
> > request.setAttribute --> Set an attribute in the life of this
> > particular
> > request
> >
> > -Tim
> >
> > Charles So wrote:
> >> Hello, I come across a problem with TC4.1.24 and hope fellow list
> >> users
> >> can help me...
> >>
> >>
> >>
> >> I set a variable in a servlet by calling:
> >>
> >> request.setAttribute("itemnumber",
> >> Integer.valueOf(Integer.toString(itemnumber)));
> >>
> >>
> >>
> >> A JSP will then detect if this variable exists or not by:
> >>
> >> if ( request.getParameter("itemnumber") != null ) {
> >>     try {
> >>         itemnumber = 
> >> Integer.parseInt((String)request.getParameter("itemnumber"));
> >>     } catch (NumberFormatException ex) {
> >>         System.out.println("No");
> >>     }
> >> }
> >>
> >>
> >>
> >>
> >> I have two different web browsers opened (say, one IE and 
> one Opera), 
> >> and both eventually will come to the page containing the 
> code above.
> >>
> >> The strange thing is that if the variable "itemnumber" is 
> set via IE, 
> >> the same "itemnumber" will be seen at Opera!!!!!
> >>
> >> This shouldn't happen as both have different session ID, and are 
> >> completely unrelated.
> >>
> >>
> >>
> >> Who has come across this problem? How can I work around this?
> >>
> >> Thanks!
> >>
> >>
> >
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message