• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling servlet on page load

 
John Schretz
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Schretz
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Again Bear, you have been a great help

John
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic