• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Embarrassing question about geting data to show up "all the time"

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this is normal behavior, so I'm kindof embarrassed to ask.

I have a jsp with a dropdown that gets populated from the db.
When I go directly to the jsp via typing in the address or clicking the link, the data doesn't load. The service layer never gets called. My list/dropdown is empty.

But if I use another servlet that loads the bean (and thus this list) into the session, it works fine. They're not even named the same thing, so it's not like I'm getting the other reference out of the session.
I have the loading in the list inside the controller class in the constructor -- and the controller gets instantiated inside the init() method of the servlet.

I feel like I'm missing something here.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It certainly sounds as if something odd is going on.

Your page controller should be able to fetch the data for the JSP without relying on other resources.

Are you fetching the data on each request that needs it? Or only once and caching it?
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My intent is that I do it once. The list I'm accessing is static using instance accessors.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By once I mean once per request, but it should get loaded the first time the controller loads it into the first instantiated bean because it's static.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know you have limitations because this is a class assignment, but for data that doesn't change, I'd read it in a context listener and store it in application scope. That way, once the web app is loaded, the data is readily available to all resources without any further headaches.

I avoid static at all costs. In a web app, the scopes are the best place to store data.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what a context listener is, but it sounds like some guy who listens for the app to load and pops all the prerequisite stuff into the scope.

This is data that changes. It's an inventory list. It gets a new object added to it every time something gets added to inventory. I actually need this same list for two screens. Once to view the inventory and once to edit a member of the inventory. I can't see the list until I "add" a member to my inventory. I think there's an order of operation thing I'm missing.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:I don't know what a context listener is, but it sounds like some guy who listens for the app to load and pops all the prerequisite stuff into the scope.


Yup.

This is data that changes.


In that case, I'd just read it in the page controller in the doGet() and pass it to the JSP as a scoped variable.

Not sure where a constructor would be involved in all this.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do that.... at least I thought I did.... it's not working when I load the page the first time. Do I need to do something special to get the "get" method to run, or just visit the jsp?

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhhh I think I see the problem now. I had a problem using Lists in my useBean so I started using the static list and the getter. Let me see if I can rearrange it again.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh. Now my list is always empty. Even when I add stuff to it.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:I do that.... at least I thought I did.... it's not working when I load the page the first time.


Are you addressing the JSP page, or the servlet? Should always be the latter.

Do I need to do something special to get the "get" method to run, or just visit the jsp?


Never just visit the JSP. In fact, it's frequently recommended to put the JSPs in a folder under WEB-INF so that they cannot be directly addressed without their controller.

Your code doesn't look like it will compile as allCharacters isn't defined. I assume it's a List of something?

But if allCharacters is correctly set at this point, I don't see much reason it shouldn't work.

I'm a tad concerned about what's going on under the covers to obtain the list. You've used words like "static" and "constructor" that don't quite jive to me.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm a tad concerned about what's going on under the covers to obtain the list. You've used words like "static" and "constructor" that don't quite jive to me.


They don't jive because I have no idea what I'm doing. :)

I started out trying to get a List the same way I got a single object, then iterate through the list and move on with my life. When I visited the page, it threw exceptions about types and couldn't find java.util.List so I assumed I did it wrong.
So I stopped trying to put the list into the session and instead return a bean that had the list on it and just use the getter to get the list. The list is a list of "all of this kind of bean" so I thought it best to make it static. That's where I was last night and you told me static getters dont get along with the bean spec.
The list was always empty.
So I thought I wasn't ever accessing the service layer, so I tested another function, and in doing so, the list was becoming populated for my broken and sad dropdown, because I have a controller that when it gets instantiated I used it to populate that list for me, and, well, it being static, when I useBean, I get a reference to the list.... even if it's a bean I've never touched.

I understand this probably isn't good. That's why I'm here. Looking back on the last 3 weeks of struggling, I should have asked for help sooner.

The first function we created (which I modeled the rest on), we created a plain html/jsp file with a plain form and called the post on a servlet and it landed me onto the jsp I wanted. Talking about this is making me realize this is where I'm confused. I started off by putting everything inside META-INF/jsp because I thought that was good, but I couldn't access stuff right so I moved everything into the root of my app. It worked, so I thought I did good.

I don't know how to go to a plain jsp file and get the data from the database, then use that same jsp to submit a post request to the servlet. Or even if that's what I want. I feel like if I had a plain page and the user typed in a name I could retrieve that just like on the login.... but the spec requires a dropdown. And I've come so far on this that I don't want to bail when it works half the time. And I understand it only works because I hacked it to work.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At this point I don't think your problem is in the JSP or servlet, but in how the list is being obtained under the covers.

And all of that should be completely independent of JSP or servlets so it's just plain old Java.

Maybe that's what we need to look at next.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think when we're discussing how the list gets populated this is what we're talking about.

I started off with this.... which I couldn't get to work.



This didn't work, so I added this:


And now it only works after I use another function which uses a SkylanderBean... because it's a static list here:



Here's the piece of Hibernate inside my service -- this works:

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why all the static hoopla? Just get the list via Hibernate and return it.

You know, that whole KISS thing! :cool:
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try to return the list, I load it into the session and the page blows up complaining it can't find java.util.List..... let me try again.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the error:

message /update.jsp (line: 5, column: 0) The value for the useBean class attribute java.util.ArrayList<model.domain.SkylanderBean> is invalid.

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /update.jsp (line: 5, column: 0) The value for the useBean class attribute java.util.ArrayList<model.domain.SkylanderBean> is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1234)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1182)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Generator.generate(Generator.java:3490)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, two things:
  • Put it in the request, not the session. It only is needed for the current request. (But that looks like what you are already doing in the servlet.)
  • Why are you using useBean at all? Not needed for the JSTL and EL (which I see you are using in your JSP).
  •  
    Janeice DelVecchio
    Bartender
    Posts: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok now it's not broken, just empty. Actually null because when I try to "add" it throws a null pointer.

    Why does it work without the use bean? It just pulls the guy with that name out of the scope?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Janeice DelVecchio wrote:Ok now it's not broken, just empty. Actually null because when I try to "add" it throws a null pointer.


    add?

    Why does it work without the use bean? It just pulls the guy with that name out of the scope?


    Exactly. The useBean directive creates scripting variables for use with obsolete scriptlets. Not needed in modern JSPs.

    Sorry, but gotta head to bed. Talk at ya tomorrow.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic