Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Servlet as an index page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet as an index page?" Watch "Servlet as an index page?" New topic
Author

Servlet as an index page?

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi. My index page (index.jsp) is supposed to be generated dynamically everytime is accessed.
1o.) Index will do some lookup into the database and retrieve information to complete the information in the web page. the connection, sql's, beans are done in a servlet. So, how do I send the request to the servlet inmediately when the page is requested?
if you need more clarifications, let me know...
cheers


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
This is just a quick answer off the top of my head but you should be able to use a RequestDispatcher to immediately sent the request that has accessed the jsp to a servlet. I've only done this the reverse way (servlet -> jsp) but it should work the other way.
Thanks,
Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

You can also achieve this via HTML by including the following in the head section of your index.html:

This, however, uses a bit more overhead than Steve's method (since an extra request is generated).
hth,
bear
[ April 18, 2002: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
thanks guys. But is it a situation like this normal? I'd think is a bit overhead...
any suggestions and opinions??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

I see this sort of thing quite often. If you are worried about overhead, use Steve's RequestDispatcher forward solution where the overhead will be minimal.
hth,
bear
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
hhmm.. so the request dispatcher will be like the first java code in my index page???
thx
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
You mean more precisely: your first scriptlet in your jsp? Cause you allready use beans.
And scriptlets should be avoided. You might write some custom tags or a bean to access to do the database lookups and the bean initialisation.
[ April 18, 2002: Message edited by: Axel Janssen ]
mustang india
Ranch Hand

Joined: Feb 05, 2002
Posts: 60
U can directly call the servlet itself, why do u want to call the JSP. I mean to say, if ur servlet is IndexServlet, u can map it to "index" and ur URL would be "http://xyz.com/webapp/index" this url will invoke the servlet which dose all the JDBC connection etc.and forwards to a JSP.
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
The servlet mapping, if it works and I can't see why not, will probably be the best solution to hit the servlet directly. My only concern would be the situation where the user requests the following:
www.someserver.com
or
www.someserver.com/
Will the webserver resolve these to /index.htm(l) and as a consequence pass the request to the servlet container as a matter of course?
Also, remember that you can see the welcome page to something other than index.html in the web.xml config file
Cheers,
Steve
[ April 19, 2002: Message edited by: Steve Granton ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hello everyone and thanks for your response.
Sounds like a good idea what "mustang india" (I don't think this is your real name ) proposed, to hit the servlet directly.
I'm a bit new with this situation, could you please show me a bit of code so I can get in the correct direction? Or should I just modify the web xml file and specify that the index.htm will be mapped to the servlet??
thankssss
 
jQuery in Action, 2nd edition
 
subject: Servlet as an index page?
 
Similar Threads
Difference between %include file=""% and jsp:include file=""
index.html hits my servlet!
In a MVC design is it possible to automatically invoke a Command upon page load?
Encapsulation problem
I'm new to sevlets help