aspose file tools*
The moose likes Servlets and the fly likes Calling servlet on page load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling servlet on page load" Watch "Calling servlet on page load" New topic
Author

Calling servlet on page load

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Hi i have a list of brand names and image namse that i need to get from a database on the home page of my site. There is no button that will send the request. I need to send the request when the page is loading. I am a little confused on how to accomplish this.

I searched the web and found:<%request.getRequestDispatcher("dispVendors").include(request,response);%>

This calls my servlet. I have debugged it in eclipse but i am not sure how to send the arraylist back after it is filled.

1. Could I do this by creating an index.jsp page the when you go to the main URL, this page calls the servlet that then forward all the info i need to Home.jsp? or is there a better way to do this?

2. Is there a way to, in one page when it loads request data from a servlet and have it returned to the same page before it fully loads?

3. Going forward i would like to dynamicaly generate meta tags from the DB so as each page loads i would like to get that info from the DB and add it to the head of my jsp.

maybe i am just looking at this the wrong way, just looking for some direction.

Thanks,
John

JSP


SERVLET
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61623
    
  67

You're doing it backwards. Your page should not be calling a servlet,rather, your servlet should be gaining control first, then forwarding to the page to render the view.

Perhaps this article can help you understand proper and modern web application stucture.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Ok, I see what you mean. I know how to do it when you have an action button. But in this case its the home page. So would the best way be to create an index.jsp page that calls my servlet, then forwards it to lets say home.jsp which displays my home page with all the info i need?

Thanks,
John
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61623
    
  67

If you are using a modern version of servlets, you can set up your "welcome file" to be a servlet. Or, yeah, just put an index.jsp in the root folder that redirects to the servlet URL.
[ September 10, 2008: Message edited by: Bear Bibeault ]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Ok, thanks Bear, this make things alot clearer.

so as far as the meta tags i can have my index.jsp call my servlet which can make a DB call to fill my array list with vendors, then get my meta tag data from the db and send both back in the response by:

session.setAttribute("vendors", vendors);
session.setAttribute("metadata", metadata);

then in my jsp i cn use session.getattribute("metadata") to write my tags and the for loop to fill in my vendors?

Is my understanding correct?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61623
    
  67

I'm not quite sure what you mean by "meta tag data from the db", but yes, in order to "pass" data from the servlet to the JSP, you would create scoped variables with setAttribute() which can then be referenced on the JSP using the EL (Expression Language). Avoid any Java code on the JSP -- that's the old way of doing things.
in my jsp i cn use
Please avoid using abbreviation like "cn" for "can". Thanks.
[ September 10, 2008: Message edited by: Bear Bibeault ]
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Please avoid using abbreviation like "cn" for "can". Thanks.


sorry typo, sometimes my fingers are faster than my brain.

As far as the meta tags. What i want to do i have a table in a DB that holds all my keywords and such for the meta tag data for each vendor. then when my servlet is called and the page is forwarded i want to put the appropriate meta tags on the top of that page.
[ September 10, 2008: Message edited by: John Schretz ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61623
    
  67

Ah, ok. No problem. All the HTML markup is just template text, so you can put dynamic data anywhere you want within the page -- the meta tags have no special significance to the JSP processing.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
Thanks Again Bear, you have been a great help

John
 
Don't get me started about those stupid light bulbs.
 
subject: Calling servlet on page load