• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to call a servlet constructor/method upon redirecting to another page?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have login.jsp and home.jsp.
Each have its own servlets.

When I click Login button from login.jsp, it will navigate to home.jsp.

But before displaying home.jsp from he user, I want to call homeServlet.java's constructor/method.

This is because I want to populate a dropdown from home.jsp from a database.

I'm using MVC2 Servlet/JSP/Bean framework and AMAP, I do not want to execute javascripts/jquery for populating dropdowns.
Thanks!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets should never have constructors, but you can add additional methods that can be called from within doGet/doPost.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks for your response.

Can you suggest how can I populate a select item list via java code and not scriptletwhen the page is loaded?


Thanks!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fetch the data in the controller that you use to access the JSP, and set it as a List (or Map) in a request attribute. Then you can use a JSTL loop to iterate over the contents of that attribute in the JSP, and generate HTML as appropriate. See http://www.ibm.com/developerworks/java/library/j-jstl0318/ if you're not familiar with JSTL.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a little more advanced, but this might be an opportunity for a custom tag. I've written several for the purpose of building select/option controls that we reuse such as a list of employees, machine names, part number codes, etc.
 
Tim Holloway
Bartender
Posts: 18715
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any app that has a "login method" in it is one that can probably be easily hacked.

But regardless, what you are trying to do is construct an object that will be shared between all users for the life of the web application.

Stuff like shared menus is stuff that I create as objects in Application Scope. You can then access then from the ServletContext, and, of course, JEE has built-in support for locating application scope objects in JSPs and similar components.

You can build the menu and store it in a ServletContextListener startup method at webapp startup time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!