• Post Reply Bookmark Topic Watch Topic
  • New Topic

incorporating database info on webpage loading  RSS feed

 
mikey Rodgers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want the home page of a website I'm developing to contain information from the database e.g. when you go to a news website such as CNN or the BBC the headlines automatically load. I want to achieve the same result.

In the book I've learnt servlets from (not the Head First one though I do have two Head First books!), it's always the case that you have to click on a link that activates a servlet that then contacts the EJBs and the database, and then redirects you to another jsp. This isn't the case here.

Suppose I have a plain javabean that is instantiated everytime the home page loads and there is code inside the javabean that uses a servlet to contact the database. This javabean's getters and setters are then used to provide all the necessary info that the home page may need. Is that the right approach?

Mike.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you envision this "java bean" running in the browser? That's what it sounds like, since you say it would run "when the homepage loads". The browser only supports Java in the form of applets, i.e. not Java beans. It would be possible for the applet to communicate with a servlet on the server, I suppose.

Or do you envision the "java bean" running on the server? If that's the case then you have it backwards. The servlet would call on the java bean to get the data from the database, and the servlet would use those results to generate the page. Just like it says in the book, except it isn't mandatory to use EJBs to access the database.

Or did you mean something else? You seem to have rejected the model in the books in favour of something that you don't understand. You say "this isn't the case here" without explaining why it isn't or what is the case instead.
 
mikey Rodgers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you are getting confused between a javabean and an EJB? They are two totally different things. Forget EJBs for the moment and assume they are not being used.

I mentioned a javabean simply because it is an object that can be used to store some information (in this case, information from the database) that the jsp can then access.

But I'm not sure exactly how it would work or indeed if there is a better design available, hence the post.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I know the difference.

Typically your page would be generated like this: a servlet runs. It creates one or more Java beans which do something, then it forwards to a JSP that uses the Java beans to produce HTML. But that's exactly what the book said. If you're asking what's the right approach, this is the right approach. Did you have some reason to mistrust what the book was saying?
 
mikey Rodgers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response but I'm still not sure how to go about including data from the database in the home page. How do I access the database whilst remaining in the jsp for the home page?
 
mikey Rodgers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance in PhP you simply have the code to access the database at the top of the script, the data is retrieved, and then presentated at the bottom of the same script.

One workaround method with the homepage.jsp could be to have a variable in it that is initially set to null, say. Then if the variable equals null you are directed to a servlet which accesses the database. Then you are redirected to the homepage.jsp with the data and with a non-null value for this variable to prevent you being sent back to the servlet.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't access the database while "remaining" in the JSP. Well, you can, but that's considered bad taste. You start with a servlet that accesses the database and reads the data from the database into some data structure. Just what data structure you should use depends on the data in question. Then your servlet forwards to the JSP, which takes data from that structure and converts it into HTML.

If you don't know how to get a servlet to access a database, then go off and read Sun's JDBC tutorial. (Servlets are written in Java so don't get thrown off by the fact that they have a name.) Sun also has a Collections tutorial which explains how to use the various types of collections.

And stop trying to think of "workarounds". Workarounds are for where there isn't an easy way to do something. In this case there is an easy way and you're just making things difficult for yourself. Your particular example is wrong because your home page isn't the JSP. It's the servlet. So when somebody links to your home page, the servlet starts running, it accesses the database, then forwards to the JSP which generates the page. The user knows nothing of the JSP and never clicks on a link to it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!