Java Forum / General / June 2005
my first session bean
ian ward - 21 Jun 2005 11:14 GMT I am exploring what I can do with my first page of jsp. I have included a useBean action which allows me to access the state of a class so that I can display it. If the bean has session scope then, when I call the page several times, I get repetitions of the displayed data. It is as if the page is not 'reinitialised'. In fact, it seems that, with each transformation into html, the previous transformations of java code are retained rather than the page being regenerated from scratch. This doesn't happen if the bean has request scope. Is this the normal behaviour for a jsp page which uses a session bean? I had understood that the scope of a bean determines its availability but wouldn't normally impact the construction of a page which declares/uses it.
Thanks Ian
Wendy Smoak - 21 Jun 2005 15:12 GMT > If the bean has session scope then, when I call the page several times, > I get repetitions of the displayed data. It is as if the page is not [quoted text clipped - 6 lines] > but wouldn't normally impact the construction of a page which > declares/uses it. Sounds normal to me. If you put something in session scope, it stays there. What is this "reinitialization" that you think should be happening? Why don't you post some of the JSP, the output, and explain what you [thought would|wanted to] happen.
 Signature Wendy Smoak
ian ward - 21 Jun 2005 19:53 GMT OK, well here's the page.......
<%@ page import = "beans.SystemBean, businessservices.Programme, java.util.List, java.util.ListIterator" %> <jsp:useBean id="dialogerSystem" class="beans.SystemBean" scope="request"/>
<HTML> <BODY BGCOLOR="white">
<% String s_userid=request.getParameter("userid"); String s_password=request.getParameter("password"); %>
The user id is <%= s_userid %> <br>
<% List theProgrammes; Programme aProgramme; String did_it_work = new String();
if (!dialogerSystem.logOn(s_userid,s_password)) { did_it_work = "failed"; } else did_it_work = "succeeded"; session.setAttribute("logonStatus",did_it_work);
if (did_it_work == "failed") { %>
<jsp:forward page="/dialoger/jsp/logon.jsp"/>
<% } else { %>
logon ok - we're going to list the programmes available to this user <br><br>
<% theProgrammes = dialogerSystem.getTheProgrammes(); for (ListIterator l = theProgrammes.listIterator();l.hasNext();) { aProgramme = (Programme)l.next(); %>
<a href="/dialoger/jsp/programmeContents.jsp">
<% out.println(aProgramme.getName()); %>
</a> <br>
<% } } %> clicking on one of these above will normally display its contents on the next page
</BODY> </HTML>
...in this version the scope is 'request'but the rest of the code is unchanged....I think the critical line is the out.println where a name, having been retrieved from a list belonging to the bean, is displayed.
It seems logical to me to expect that a page, if rerun without having been changed, should do the same thing. The bean, in session scope stays there of course but it shouldn't change just 'cos you've accessed some property or other. I want it to have session scope 'cos I might use it elsewhere on another page, just like the session attribute I've created. Can you explain what you mean by putting something in session scope?
thanks Ian
Andrew Thompson - 21 Jun 2005 20:20 GMT ...
> <jsp:useBean id="dialogerSystem" class="beans.SystemBean" > scope="request"/> ...
> Can you explain what you mean by putting something in session scope? Well, I'm not Wendy, but..
<jsp:useBean id="dialogerSystem" class="beans.SystemBean" scope="session"/>
See - <http://java.sun.com/products/jsp/tags/10/syntaxref10.fm14.html>
HTH
 Signature Andrew Thompson http://www.PhySci.org/codes/ Web & IT Help http://www.PhySci.org/ Open-source software suite http://www.1point1C.org/ Science & Technology http://www.LensEscapes.com/ Images that escape the mundane
ian ward - 21 Jun 2005 22:00 GMT I think I'm finding your second contribution a bit more useful than your first. Having read it through I'm wondering if it implies, on repeated reloading of the page, the creation of several variables of the same name pointing at the same instantiated object (I had thought before reading this that the object may be duplicated) - and then, for some strange reason, the 'use' of all of these when the object's attribute is accessed/displayed via the variable(s). Seems unlikely? My problem seems to go a bit further than just understanding the meaning of 'session scope' - as I first asked, does it have something to do with the way the html page is generated? Maybe I expressed myself badly.
Ian
Andrew Thompson - 22 Jun 2005 06:04 GMT > I think I'm finding your second contribution a bit more useful than > your first. Note that two people have each contributed a single reply to your post.. (before this post - obviously)
> ..My problem > seems to go a bit further than just understanding the meaning of > 'session scope' .. Aahh.. I probably led the thread off on a tangent then.
I will bow out of this thread now and 'hand the reigns' back to the servlet guru's - like Wendy!
 Signature Andrew Thompson http://www.PhySci.org/codes/ Web & IT Help http://www.PhySci.org/ Open-source software suite http://www.1point1C.org/ Science & Technology http://www.LensEscapes.com/ Images that escape the mundane
Wendy Smoak - 22 Jun 2005 17:38 GMT > Having read it through I'm wondering if it implies, on repeated > reloading of the page, the creation of several variables of the same > name pointing at the same instantiated object [...] and then, for some > strange reason, the 'use' of all of these when the object's attribute > is accessed/displayed via the variable(s). Seems unlikely? I'm having trouble figuring out what your question is. Can you restate it?
I suggest that you read the Servlet and JSP specifications. They will tell you exactly what the container is required to do for you, (and some things it may/must not do also.) You also need a good handle on the HTTP request/response cycle. No offense intended if you already know this stuff...
http://www.servlets.com/ is a good reference page.
 Signature Wendy Smoak
ian ward - 22 Jun 2005 21:15 GMT Yes, I'm being a bit intuitive about this, trying to avoid a mountain of reading for what is a reasonably simple try-out.
I have tried to ask my question as clearly as possible. I've also tried to make some semi-intelligent guesses about what seems to be happening based on what (little) I do know. If what I've described doesn't seem odd to you then I think I probably do need to go into this a bit more as it doesn't seem very logical to me.
Thanks anyway Ian
John C. Bollinger - 24 Jun 2005 04:23 GMT > Yes, I'm being a bit intuitive about this, trying to avoid a mountain > of reading for what is a reasonably simple try-out. [quoted text clipped - 4 lines] > odd to you then I think I probably do need to go into this a bit more > as it doesn't seem very logical to me. 1) You do not need to use beans to use JSP.
2) If you do use beans in your JSP, you need to understand what the various values of the scope attribute mean:
+ application: A bean instance associated with the specified id is sought in the application (via its ServletContext). If found, that instance is used in the page, otherwise a new instance is created, stored in the application and used in the page as it services the current request. The same instance may later be used by any page or servlet in the same webapp.
+ session: A bean instance associated with the specified id is sought in the session (via its HttpSession). If found, that instance is used in the page, otherwise a new instance is created, stored in the session and used in the page as it services the current request. The same instance may later be used by any page or servlet in the same webapp, while servicing another request in the same session. Other distinct bean instances may be associated with the same name in other sessions.
+ request: A bean instance associated with the specified id is sought in the request (via its HttpServletRequest). If found, that instance is used in the page, otherwise a new instance is created, stored in the request and used in the page as it services the current request. The same instance may later be used by any page or servlet in the same webapp, while servicing the same HTTP request (which can happen when a JSP forward or include is used -- but do not confuse that with an HTTP redirect). Other distinct bean instances may be associated with the same name in other requests.
+ page: A bean instance created and associated with the specified id in the context of the JSP in which the declaration appears for the duration of that page's handling of the current request. Every invocation of the page will cause a distinct bean instance to be created and used, and the bean instances will not be visible to other pages.
3) I personally find it helpful to keep in mind that JSP pages are internally translated into Java source for servlets, then compiled to classes for use by the container. Understanding how JSP syntax and features map to Java code guides me through some of the intricacies of JSP. At least some knowledge of these correspondences is essential if you use JSPs and servlets together in one web application.
4) The ultimate reference material for JSP is the JSP specification, available free from Sun. The most recent version is 2.0. You may also want to obtain the Servlet spec, also available from Sun; its most recent version is 2.4. The size of these specs is a bit daunting, but they contain a great deal of material that you can skip over if you are just trying to orient yourself. For servlets, you may also want to get the HTML version of their API specification; it is part of the J2EE API spec.
 Signature John Bollinger jobollin@indiana.edu
ian ward - 24 Jun 2005 08:14 GMT John, Many thanks for your clear explanations. As I said to Wendy, I have an understanding of these concepts which is perhaps less precise than both the full spec (which I appreciate is the final reference) and what you have given me here. My original query was based on that understanding and I was reasonably sure, according to my current grasp of JSP, that the behaviour I was seeing was not normal. I understand why those who replied to my query didn't seem to connect with the issue because, as it turns out, it was not in the JSP, nor, I believe, in the scope attribute used (although this may or may not 'reveal' the underlying problem). I developed my Java code with certain scenarios in mind. The use of that code from another level of program control (ie the browser) introduces possibilities which I will have to allow for. In this particular case, code was being rerun which 'shouldn't be' and 'state' was being reloaded into my bean. I had thought that perhaps I was seeing something to do with your point 3 but in fact it was back in the bean and I simply needed to ensure that the state of my bean is 'always' is it should be when I call the code concerned. Ian PS You don't fancy having a stab at my 'totally unexplainable tomcat mystery' do you?!:) (warning, the issue changes halfway through the thread!)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|