Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet as an index page?

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Granton
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hhmm.. so the request dispatcher will be like the first java code in my index page???
thx
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic